Page 1 of 1

สอบถามการใช้ Combobox ดึงข้อมูลมาแสดงใน Listbox

Posted: Mon Nov 27, 2023 12:27 pm
by tigerwit
จากไฟล์ที่แนบมา
ต้องการใช้ Combobox ค้นหาข้อมูลมาแสดงใน listbox บน userform
โดยมีโค๊ดดังนี้
Code 1

Code: Select all

Private Sub UserForm_Initialize()
Dim i As Long
For i = 1 To Sheet2.Range("A301").End(xlUp).Offset(1, 0).Row
Me.ComboBox1.AddItem Sheet2.Cells(i, 1).Value
Next i
End Sub
คำถามข้อ 1 จากโค๊ดด้านบนหากต้องการให้ Combobox แสดงรายการเฉพาะที่มีปรากฎอยู่ใน ชีท Sheet2 จะต้องปรับโค๊ดอย่างไร (ได้ทำช่วงข้อมูล Dynamic Range ชื่อ Number ไว้แล้ว)

Code 2

Code: Select all

Private Sub ComboBox1_Change()
Sheet1.Range("N1") = ComboBox1.Text
Dim a
Dim i As Long
Me.ComboBox1.Text = StrConv(Me.ComboBox1.Text, vbProperCase)
Me.ListBox1.Clear
For i = 3 To Application.WorksheetFunction.CountA(Sheet1.Range("D:D"))
a = Len(Me.ComboBox1.Text)
If Left(Sheet1.Cells(i, 1).Value, a) = Left(Me.ComboBox1.Text, a) Then
Me.ListBox1.AddItem Sheet1.Cells(i, 1).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 4).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 5).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 6).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 7).Value
End If
Next i
คำถามข้อ 2 หากต้องการให้ Listbox แสดงรายการจำนน 4 คลอลัมน์ ประกอบด้วย รายการ,หน่วยนับ,ราคาต่อหน่วย,จำนวน (ไม่ให้แสดง เลขที่บันทึก) จะปรัโค๊ดอย่างไร

หรือหากไม่ปรับตามข้อ 2 ปล่อยไว้แบบนั้น
จะต้องปรับโค๊ดด้านล่างนี้อย่างไรให้สามารถส่งข้อมูลไปไว้ที่ชีท Sheet3 โดยไม่ต้องเอาเลขที่บันทึกไปวางด้วย

Code: Select all

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    ActiveCell.Value = ListBox1.List(ListBox1.ListIndex)
    ActiveCell.Offset(, 1).Value = ListBox1.List(ListBox1.ListIndex, 1)
    ActiveCell.Offset(, 2).Value = ListBox1.List(ListBox1.ListIndex, 2)
    ActiveCell.Offset(, 3).Value = ListBox1.List(ListBox1.ListIndex, 3)
    ActiveCell.Offset(, 4).Value = ListBox1.List(ListBox1.ListIndex, 4)
    ListBox1.Value = ""
    ActiveCell.Offset(1, 0).Select
End Sub

Re: สอบถามการใช้ Combobox ดึงข้อมูลมาแสดงใน Listbox

Posted: Tue Nov 28, 2023 2:48 pm
by puriwutpokin
ตอบข้อ 1 ครับ

Code: Select all

Private Sub UserForm_Initialize()
Dim i As Long
For i = 1 To Application.CountIf(Sheet2.Range("a:a"), "?*")
Me.ComboBox1.AddItem Sheet2.Cells(i, 1).Value
Next i
End Sub
ตอบข้อ 2

Code: Select all

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    ActiveCell.Value = ListBox1.List(ListBox1.ListIndex)
    ActiveCell.Offset(, 1).Value = ListBox1.List(ListBox1.ListIndex, 1)
    ActiveCell.Offset(, 2).Value = ListBox1.List(ListBox1.ListIndex, 2)
    ActiveCell.Offset(, 3).Value = ListBox1.List(ListBox1.ListIndex, 3)
'    ActiveCell.Offset(, 4).Value = ListBox1.List(ListBox1.ListIndex, 5)
    ListBox1.Value = ""
    ActiveCell.Offset(1, 0).Select
End Sub

Code: Select all

Private Sub ComboBox1_Change()
Sheet1.Range("N1") = ComboBox1.Text
Dim a
Dim i As Long
Me.ComboBox1.Text = StrConv(Me.ComboBox1.Text, vbProperCase)
Me.ListBox1.Clear
For i = 3 To Application.WorksheetFunction.CountA(Sheet1.Range("D:D"))
a = Len(Me.ComboBox1.Text)
If Left(Sheet1.Cells(i, 1).Value, a) = Left(Me.ComboBox1.Text, a) Then
Me.ListBox1.AddItem Sheet1.Cells(i, 4).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 5).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(i, 6).Value
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(i, 7).Value
'Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(i, 7).Value
End If
Next i

End Sub

Re: สอบถามการใช้ Combobox ดึงข้อมูลมาแสดงใน Listbox

Posted: Fri Dec 01, 2023 3:56 pm
by tigerwit
ขอบคุณครับ