การใช้คำสั่ง vba Excel อธิบายการทำงานแบบเข้าใจง่ายๆ ในการสร้าง UserForm เพื่อ ป้อน ข้อมูล ทั้งแบบ textbox และแบบตัวเลือก option button ลง worksheet ของ Excel สำหรับโคํดที่ใช้ในคลิป ตามข้างล่างนี้ ลองประยุกต์ใช้กันดูนะค่ะ
Private Sub CommandButton1_Click()
Do
r = r + 1
Loop Until Cells(r, 1) = ""
Cells(r, 1) = TextBox1.Text
Cells(r, 3) = TextBox2.Text
If OptionButton1.Value = True Then
Cells(r, 2) = "Man"
Else
Cells(r, 2) = "Woman"
End If
TextBox1.Text = ""
TextBox2.Text = ""
OptionButton1.Value = False
OptionButton2.Value = False
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
OptionButton1.Value = False
OptionButton2.Value = False
End Sub
การสร้างโปรแกรมเพื่อแก้ไขข้อมูลโดยใช้ฟอร์ม (UserForm) ฟังก์ชั่น if , match และสามารถค้นหาข้อมูลพนักงาน ด้วย function For, Vlookup และ ใช้คำสั่งโค้ด VBA Excel สำหรับปุ่มการแก้ไขข้อมูล ดังนี้
Dim y As Long
Dim x As Integer
x = MsgBox("Do you want to update?", vbOKCancel, "Progam")
If x = vbOK Then
y = Application.Match(Me.ComboBox1.Text, Range("A1:A13"), 0)
Cells(y, 2) = TextBox1.Value
Cells(y, 3) = TextBox2.Value
Cells(y, 4) = TextBox3.Value
Cells(y, 5) = TextBox4.Value
Cells(y, 6) = TextBox5.Value
Cells(y, 7) = TextBox6.Value
Cells(y, 8) = TextBox7.Value
Cells(y, 9) = TextBox8.Value
Else
Dim q, p As Long
q = Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
For p = 1 To q
Me("textbox" & p).Value = Application.WorksheetFunction.VLookup(ComboBox1.Value, Sheet1.Range("A2", "G" & q), p + 1, 0)
Next p
End If