Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

excel - Data Entry form with vba codes

I have an excel form in one sheet as data entry (1- Textbox1 = Name, 2-Textbox2 = Old, 3-Textbox3= Email )

question from:https://stackoverflow.com/questions/65870041/data-entry-form-with-vba-codes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You do not say anything to clarify my question... When come here and need help, it is good to clarify any question and if you do not understand what is it about to ask for clarifications.

Anyhow, I am trying helping you suggesting the next scenario:

  1. I assumes that you name "an excel form in one sheet" the sheet itself, having three text boxes (ActiveX type) and a combo box (also ActiveX type).

The three text boxes will have the name "TextBox1", "TextBox2" and "TextBox3". If not so, the code which will follow would be easy to adapt at your real names.

The combo box name should be "ComboOptions". After creation, when in Design mode, right click on it, choose Properties and set A1:A2 at its ListFillRange property. Then add "Teachers" in "A1" and "Students" in "A2.

  1. Paste the next code in a standard module:
Private Sub EnterButton_Click()
 Dim sh As Worksheet, sh2 As Worksheet, sh3 As Worksheet, workSh As Worksheet, lastRow As Long
 Dim cbOption As MSForms.ComboBox, txtName As MSForms.TextBox, txtOld As MSForms.TextBox, txtEmail As MSForms.TextBox

 Set sh = ActiveSheet
 Set sh2 = sh.Next 'Worksheets("Teachers")
 Set sh3 = sh2.Next 'Worksheets("Students")

 Set cbOption = sh.OLEObjects("ComboOptions").Object

 If cbOption.Value = "" Then MsgBox "No option chosen in combo box...": Exit Sub
 If cbOption.Value = "Teachers" Then
    Set workSh = sh2
 ElseIf cbOption.Value = "Students" Then
    Set workSh = sh3
 End If
 Set txtName = sh.OLEObjects("TextBox1").Object
 Set txtOld = sh.OLEObjects("TextBox2").Object
 Set txtEmail = sh.OLEObjects("TextBox3").Object
 If txtName.Text = "" Or txtOld.Text = "" Or txtEmail.Text = "" Then _
       MsgBox "All involved text boxes must have a value!": Exit Sub
 lastRow = workSh.Range("B" & workSh.rows.count).End(xlUp).row + 1
    With workSh
       .Range("B" & lastRow).Value = txtName.Value
       .Range("C" & lastRow).Value = txtOld.Value
       .Range("D" & lastRow).Value = txtEmail.Value
    End With
End Sub
  1. Select one option in the combo box, run the above code and send some feedback about the behavior against your expectations.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...