เรียน คุณคนควน
ได้ปรับปรุง Code ในส่วนที่ท่านแนะนำ ดังนี้
If ComboBox1.Text <> "" And checkVal > 0 Then
lastRow = .Match(CInt(ComboBox1.Text), Num, 0)
TextBox1.Text = .Index(Alldata, lastRow, 2)
TextBox2.Text = .Index(Alldata, lastRow, 3)
TextBox3.Text = .Index(Alldata, lastRow, 4)
TextBox4.Text = .Index(Alldata, lastRow, 5)
TextBox5.Text = .Index(Alldata, lastRow, 6)
ของเดิม
If checkVal > 0 Then
TextBox1.Text = .Index(Alldata, lastRow, 1)
TextBox2.Text = .Index(Alldata, lastRow, 2)
TextBox3.Text = .Index(Alldata, lastRow, 3)
TextBox4.Text = .Index(Alldata, lastRow, 4)
TextBox5.Text = .Index(Alldata, lastRow, 5)
ผลที่ได้ ดังนี้
1.เมื่อเลือกตัวเลข ที่พิมพ์ไว้แล้ว จะแสดงค่าถูกต้อง
2.ถ้าจะพิมพ์ข้อมูลเพิ่ม(ไม่ตรงกับเลขเก่า)ไม่สามารถพิมพ์ในComboBoxได้เลย
Code: Select all
Private Sub ComboBox1_Change()
Dim Num As Range 'Num:rangename Column
Dim Alldata As Range 'AllData:rangename
Dim lastRow As Integer
Dim checkVal As Long
Set Alldata = ActiveSheet.Range("AllData")
Set Num = ActiveSheet.Range("Num")
With Application
checkVal = .CountIf(Num, ComboBox1.Text)
If ComboBox1.Text <> "" And checkVal > 0 Then
lastRow = .Match(CInt(ComboBox1.Text), Num, 0)
TextBox1.Text = .Index(Alldata, lastRow, 2)
TextBox2.Text = .Index(Alldata, lastRow, 3)
TextBox3.Text = .Index(Alldata, lastRow, 4)
TextBox4.Text = .Index(Alldata, lastRow, 5)
TextBox5.Text = .Index(Alldata, lastRow, 6)
Else
ComboBox1.Text = ""
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
End If
End With
ComboBox1.SetFocus
End Sub
ขอความกรุณาต่อครับ
You do not have the required permissions to view the files attached to this post.