snasui wrote: Tue Jun 23, 2020 8:41 pm
Totem wrote: Tue Jun 23, 2020 2:46 pm
3.ข้อความหรือตัวเลขที่พิมพ์ลงไปจะไปอยู่ที่ ช่อง A1 MyAdvancedSearch() ก็จะค้นหารายการมาให้ เช่น เลือก BBB2 ใน sheet add ก็จะแสดงรายการ ชื่อ1 BBB2 แผนก1 bbb2
คือ Listbox แสดงรายการเหมือน sheet add column B , C ออกมา
ก็จะได้รายการที่ต้องการ
2.หากเปลี่ยนไปเลือกรายการใน ชื่อ แผนก 2 ก็ทำการกดปุ่ม ชื่อ แผนก 2
เมื่อแสดงรายการออกมาแล้ว เช่น ต้องการค้นหา ddddd เมือกดปุ่มค้นหา
ใน sheet add แสดงรายการ ชื่อ2 ddddd แผนก2 weeefdd5
คือ Listbox แสดงรายการเหมือน sheet add column D , E ออกมา

ทั้งหมดที่กล่าวไม่สอดคล้องกับการทำงานของฟังก์ชั่น
MyAdvancedSearch ครับ
ทั้งนี้เนื่องจาก
MyAdvancedSearch นำค่าจาก TextBox1 ไป
ค้นหาจากคอลัมน์ B คอลัมน์เดียวเท่านั้น
จึงต้องปรับ Statement นี้
For Each r In .Range("B2", .Range("B" & .Rows.Count).End(xlUp)) ใน
MyAdvancedSearch ให้สอดคล้องกับคอลัมน์ที่ต้องการค้นหาเสียก่อน เมื่อผ่านจุดนี้ไปได้แล้วติดตรงไหนค่อยนำมาถามกันต่อครับ

เรียนอาจารย์ ผมปรับ code ใน MyAdvancedSearch
โดยการกำหนดให้ใส่ข้อมูลรายการ เป็น 2 ช่อง ใน A1 กับ F1 แยกการค้นหาได้แล้ว
Code: Select all
Sub MyAdvancedSearch() 'Worksheet_Change(ByVal Target As Range)
Dim r, i As Range, v, u As String, s, a As String
Application.Calculation = xlCalculationManual
With Sheets("add")
.Range("A1,F1").CurrentRegion.EntireRow.Hidden = False
If Range("A1") <> "" Then
s = LCase(.Range("A1").Value)
For Each r In .Range("B2", .Range("B" & .Rows.Count).End(xlUp))
v = ""
v = v & r.Value
v = v & r.Offset(0, 1).Value
If Not v Like "*" & s & "*" Then
r.EntireRow.Hidden = True
End If
Next r
End If
If Range("F1") <> "" Then
a = LCase(.Range("F1").Value)
For Each i In .Range("D2", .Range("D" & .Rows.Count).End(xlUp))
u = ""
u = u & i.Value
u = u & i.Offset(0, 1).Value
u = LCase(u)
If Not u Like "*" & a & "*" Then
i.EntireRow.Hidden = True
End If
Next i
End If
End With
End Sub
และขออธิบายต่อใน userform ดังนี้
1. เมื่อกำหนด Listbox2 เลือกชื่อและแผนก 1 หรือ 2 ได้ตามต้องการ
ถ้าเลือกชื่อและแผนก 1 แล้วกดปุ่นค้นหาจะไปว่าข้อมูลลงใน A1 ค้นหาได้
ถ้าเลือกชื่อและแผนก 2 แล้วกดปุ่นค้นหาจะไปว่าข้อมูลลงใน F1 ค้นหาได้
ตามต้องการ
Code: Select all
Private Sub CommandButton6_Click() 'search
Dim LastRow&, c, x As Range, i, y As Variant
Application.Calculation = xlCalculationManual
Sheets("add").Range("A1").ClearContents
Sheets("add").Range("F1").ClearContents
If colSelect = 2 Then
Sheets("add").Range("A1").Value = TextBox1.Value
End If
If colSelect = 4 Then
Sheets("add").Range("F1").Value = TextBox1.Value
End If
' Me.ListBox2.Clear
With Sheets("add")
LastRow = Range("B" & Rows.Count).End(xlUp).Row
If Me.ListBox2.RowSource <> "" Then
Me.ListBox2.RowSource = ""
End If
Me.ListBox2.Clear
If Range("A1") <> "" Then
i = 0
For Each c In .Range("B2", Range("B2").End(xlDown)).cells.SpecialCells(xlCellTypeVisible)
Me.ListBox2.AddItem
Me.ListBox2.List(i, 0) = c.Value
Me.ListBox2.List(i, 1) = c.Offset(0, 1).Value
i = i + 1
Next c
End If
If Range("F1") <> "" Then
y = 0
For Each x In .Range("D2", Range("D2").End(xlDown)).cells.Rows.SpecialCells(xlCellTypeVisible)
Me.ListBox2.AddItem
Me.ListBox2.List(y, 0) = x.Value
Me.ListBox2.List(y, 1) = x.Offset(0, 1).Value
y = y + 1
Next x
End If
End With
Application.Calculation = xlCalculationAutomatic
End Sub
2.แล้วนำมาแสดงรายการแยกกันได้ใน Listbox2 ว่าเลือกชื่อและแผนก 1 หรือ 2 ที่ได้ค้นหาเสร็จเรียบร้อยแล้ว แต่มีปัญหากับบรรทัดนี้ครับ
Code: Select all
For Each c In .Range("B2", Range("B2").End(xlDown)).cells.SpecialCells(xlCellTypeVisible)
และ
Code: Select all
For Each x In .Range("D2", Range("D2").End(xlDown)).cells.Rows.SpecialCells(xlCellTypeVisible)
คือเลือกเฉพาะ cells B2 , D2 ลงมาจนสุดบรรทัดสุดท้าย เข้าไปกด F5 run ที่ลงขั้นตอน ทำได้ตามต้องการครับ
แต่พอ Loop ในหน้า userform แล้วมันนานค้างครับ ตามที่เข้าใจไป Loop ใน .End(xlDown)).cells ที่ไม่ใช่มาด้วย
ผมลองเปลี่ยนเป็น
Code: Select all
For Each c In .Range("B2", Range("B2").End(xlUp)).cells.SpecialCells(xlCellTypeVisible)
เลือกได้ตรงListbox2ที่ต้องการ มีแต่หัวชื่อ แผนก เท่านั้น ครับ ติดตรงนี้ครับ
3.ปัญหาการกำหนด Me.ListBox2.AddItem ให้แสดงใน Listbox2 เกินกว่า 10 column ไม่ได้ run time error 380 could not set the list property invalid property value ขอเกริ่นไว้ก่อนในช้อนี้ หากผมแก้ปัญหาข้อ 2 ได้ครับ
ขอบคุณครับ