VBA เพิ่มข้อมูลลงไปแล้วทำให้ข้อมูลที่กรอกหายไปด้วย
Posted: Thu Nov 15, 2018 9:03 am
รอกวนอาจารย์ เพิ่มโค้ด เวลาที่กรอกข้อมูลเสร็จแล้ว กดเพิ่มแล้วอยากให้ข้อมูลที่กรอกหายไปด้วยครับ
แล้วก็เวลากรอกเพิ่มข้อมูลไม่อยากให้หน้าจอมันค้างครับ อยากให้เซลมันเลื่อนลงอัตโนมัตครับผม
ตัวอย่างโค้ดครับ
Dim currentrow As Long
Private Sub cmdAdd_Click()
Dim lastrow As Long, count As Long
lastrow = Sheet1.Cells(Rows.count, 1).End(xlUp).Row
lastrow = lastrow + 1
Cells(lastrow, 1) = TextBox1
count = 0
For i = 2 To lastrow
If TextBox1 = Cells(i, 1) Then
count = count + 1
End If
If count > 1 Then
Cells(lastrow, 1) = ""
Cells(lastrow, 2) = ""
Cells(lastrow, 3) = ""
Cells(lastrow, 4) = ""
Cells(lastrow, 5) = ""
Cells(lastrow, 6) = ""
End If
If count = 1 Then
Cells(lastrow, 1) = TextBox1.Text
Cells(lastrow, 2) = TextBox2.Text
Cells(lastrow, 3) = TextBox3.Value
Cells(lastrow, 4) = TextBox4.Text
Cells(lastrow, 5) = TextBox5.Text
Cells(lastrow, 6) = TextBox6.Value
End If
Next
End Sub
Private Sub cmdClear_Click()
Dim ctl As Control
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "TextBox" Then
ctl.Value = ""
End If
Next ctl
TextBox1.SetFocus
End Sub
Private Sub UserForm_Initialize()
currentrow = 2
TextBox1 = Cells(currentrow, 1)
TextBox2 = Cells(currentrow, 2)
TextBox3 = Cells(currentrow, 3)
TextBox1 = Cells(currentrow, 4)
TextBox2 = Cells(currentrow, 5)
TextBox3 = Cells(currentrow, 6)
End Sub
แล้วก็เวลากรอกเพิ่มข้อมูลไม่อยากให้หน้าจอมันค้างครับ อยากให้เซลมันเลื่อนลงอัตโนมัตครับผม
ตัวอย่างโค้ดครับ
Dim currentrow As Long
Private Sub cmdAdd_Click()
Dim lastrow As Long, count As Long
lastrow = Sheet1.Cells(Rows.count, 1).End(xlUp).Row
lastrow = lastrow + 1
Cells(lastrow, 1) = TextBox1
count = 0
For i = 2 To lastrow
If TextBox1 = Cells(i, 1) Then
count = count + 1
End If
If count > 1 Then
Cells(lastrow, 1) = ""
Cells(lastrow, 2) = ""
Cells(lastrow, 3) = ""
Cells(lastrow, 4) = ""
Cells(lastrow, 5) = ""
Cells(lastrow, 6) = ""
End If
If count = 1 Then
Cells(lastrow, 1) = TextBox1.Text
Cells(lastrow, 2) = TextBox2.Text
Cells(lastrow, 3) = TextBox3.Value
Cells(lastrow, 4) = TextBox4.Text
Cells(lastrow, 5) = TextBox5.Text
Cells(lastrow, 6) = TextBox6.Value
End If
Next
End Sub
Private Sub cmdClear_Click()
Dim ctl As Control
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "TextBox" Then
ctl.Value = ""
End If
Next ctl
TextBox1.SetFocus
End Sub
Private Sub UserForm_Initialize()
currentrow = 2
TextBox1 = Cells(currentrow, 1)
TextBox2 = Cells(currentrow, 2)
TextBox3 = Cells(currentrow, 3)
TextBox1 = Cells(currentrow, 4)
TextBox2 = Cells(currentrow, 5)
TextBox3 = Cells(currentrow, 6)
End Sub