ผมลองใช้ vba ในการค้นข้อมูล โดยติดปัญหาที่เวลาใช้ ชืือ(name) และ นามสกุล(surname) ในการค้นพร้อมแล้ว ไม่สามารถค้นได้
code อยู่ UserForm 1 ที่ปุ่ม ค้น (Search,commandbutton1)
'case 3 name & surname search รบกวนอาจารย์หรือผู้รู้ช่วยหน่อยครับ
Code: Select all
Private Sub CommandButton1_Click()
Dim sat, sat2, s As Integer
Dim deg1, deg2, deg3, deg4 As String
With ListBox1
.Clear
.ColumnCount = 8
.ColumnWidths = "80;80;80;0;0;110;80;0"
End With
deg2 = ComboBox1.Value
'case 1 name search
If ComboBox2.Value = "" Then
For sat = 2 To Sheets("Sheet1").Cells(10000, "b").End(xlUp).Row
Set deg1 = Sheets("Sheet1").Cells(sat, "b")
If UCase(deg1) Like UCase(deg2) & "*" Then
ListBox1.AddItem
ListBox1.List(s, 0) = Sheets("Sheet1").Cells(sat, "A")
ListBox1.List(s, 1) = Sheets("Sheet1").Cells(sat, "B")
ListBox1.List(s, 2) = Sheets("Sheet1").Cells(sat, "C")
ListBox1.List(s, 3) = Sheets("Sheet1").Cells(sat, "D")
ListBox1.List(s, 4) = Sheets("Sheet1").Cells(sat, "E")
ListBox1.List(s, 5) = Sheets("Sheet1").Cells(sat, "F")
ListBox1.List(s, 6) = Sheets("Sheet1").Cells(sat, "G")
ListBox1.List(s, 7) = Sheets("Sheet1").Cells(sat, "H")
s = s + 1
End If: Next
End If
deg3 = ComboBox2.Value
'case 2 surname search
If ComboBox1.Value = "" Then
For sat = 2 To Sheets("Sheet1").Cells(10000, "c").End(xlUp).Row
Set deg1 = Sheets("Sheet1").Cells(sat, "c")
If UCase(deg1) Like UCase(deg3) & "*" Then
ListBox1.AddItem
ListBox1.List(s, 0) = Sheets("Sheet1").Cells(sat, "A")
ListBox1.List(s, 1) = Sheets("Sheet1").Cells(sat, "B")
ListBox1.List(s, 2) = Sheets("Sheet1").Cells(sat, "C")
ListBox1.List(s, 3) = Sheets("Sheet1").Cells(sat, "D")
ListBox1.List(s, 4) = Sheets("Sheet1").Cells(sat, "E")
ListBox1.List(s, 5) = Sheets("Sheet1").Cells(sat, "F")
ListBox1.List(s, 6) = Sheets("Sheet1").Cells(sat, "G")
ListBox1.List(s, 7) = Sheets("Sheet1").Cells(sat, "H")
s = s + 1
End If: Next
End If
'case 3 name & surname search
deg4 = ComboBox1.Value & ComboBox2.Value
If ComboBox1.Value = Value And ComboBox2.Value = Value Then
For sat2 = 2 To Sheets("Sheet1").Cells(10000, "b" & "c").End(xlUp).Row
Set deg1 = Sheets("Sheet1").Cells(sat2, "b" & "c")
If UCase(deg1) Like UCase(deg4) & "*" Then
ListBox1.AddItem
ListBox1.List(s, 0) = Sheets("Sheet1").Cells(sat2, "A")
ListBox1.List(s, 1) = Sheets("Sheet1").Cells(sat2, "B")
ListBox1.List(s, 2) = Sheets("Sheet1").Cells(sat2, "C")
ListBox1.List(s, 3) = Sheets("Sheet1").Cells(sat2, "D")
ListBox1.List(s, 4) = Sheets("Sheet1").Cells(sat2, "E")
ListBox1.List(s, 5) = Sheets("Sheet1").Cells(sat2, "F")
ListBox1.List(s, 6) = Sheets("Sheet1").Cells(sat2, "G")
ListBox1.List(s, 7) = Sheets("Sheet1").Cells(sat2, "H")
s = s + 1
End If: Next
End If
End Sub