Today We will learn how to insert listbox and how to populate data on ListBox by changing the keywords in TextBox. I always used Dim sh as Worksheet. By this I set the worksheet where we are working. and second step is add the listbox headers through the below code
With Me.ListBox1.AddItem "Search".List(0, 1) = "ITEM CODE".List(0, 2) = "SUPPLIER".List(0, 3) = "ITEM".List(0, 4) = "QYT".List(0, 5) = "UOM".List(0, 6) = "U / PRICE"End With
You can change as you need. Me.ListBox1.Selected(0) = True helps you to select the first row of ListBox and third step is First you need to see your worksheet From which row to serach data and upto the last row. You can find the last row by sh.Range("F" & Rows.Count).End(xlUp).Row. Now use Lcase or Ucase function that will convert the letter into lower or upper case and as per your inputed data in TextBox1 All Matched data will be displayed automatically in your ListBox.
You can follow the following code..
HOW TO POPULATE LISTBOX BASED ON TEXTBOX KEYWORDS
- Private Sub TextBox1_Change()
- Me.TextBox1.Value = LCase(Me.TextBox1)
- Dim sh As Worksheet
- Set sh = Sheets("Sheet1")
- Dim i As Long
- Dim x As Long
- Dim p As Integer
- Me.ListBox1.Clear
- With Me.ListBox1
- .AddItem "Search"
- .List(0, 1) = "ITEM CODE"
- .List(0, 2) = "SUPPLIER"
- .List(0, 3) = "ITEM"
- .List(0, 4) = "QYT"
- .List(0, 5) = "UOM"
- .List(0, 6) = "U / PRICE"
- End With
- Me.ListBox1.Selected(0) = True
- For i = 3 To sh.Range("F" & Rows.Count).End(xlUp).Row
- For x = 1 To Len(sh.Cells(i, 6))
- p = Me.TextBox1.TextLength
- If LCase(Mid(sh.Cells(i, 6), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Then
- With Me.ListBox1
- .AddItem sh.Cells(i, 6)
- .List(.ListCount - 1, 1) = sh.Cells(i, 3)
- .List(.ListCount - 1, 2) = sh.Cells(i, 4)
- .List(.ListCount - 1, 3) = sh.Cells(i, 6)
- .List(.ListCount - 1, 4) = sh.Cells(i, 7)
- .List(.ListCount - 1, 5) = sh.Cells(i, 8)
- .List(.ListCount - 1, 6) = sh.Cells(i, 9)
- End With
- End If
- Next x
- Next i
- '------------NSUTRADHAR--------------------------------
- End Sub
For Support and Feedback
You can contact if you have any queries through contact us or nsutradhar.cob@gmail.com
Please send your valuable feedback. Thanks for visiting this blogs.
To Download this file please click the below download button & wait 10 sec, you will find download link
You are doing a great job... Please I can I get the file to this video... Thank you
ReplyDeletei need this file. can i get it please?
ReplyDeleteThe code listed above does not match the code shown in the video
ReplyDeletehttps://www.youtube.com/watch?v=zaPpkWYnAN4&ab_channel=Nsutradhar
It's not clear why you turned off the capitalization search option
Private Sub TextBox1_Change()
ReplyDeleteDim sh As Worksheet
Set sh = Sheets("Sheet1")
Dim i As Long
Dim x As Long
Dim p As Long
Me.ListBox1.Clear
'FOR LIST BOX HEADER'
Me.ListBox1.AddItem "Product Name"
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = "HSN CODE"
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = "QUANTITY"
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = "RATE"
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = "GST%"
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = "TOTAL"
Me.ListBox1.Selected(0) = True
For i = 2 To sh.Range("B" & Rows.Count).End(xlUp).Row
For x = 1 To Len(sh.Cells(i, 2))
p = Me.TextBox1.TextLength
If LCase(Mid(sh.Cells(i, 2), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Then
With Me.ListBox1
.AddItem sh.Cells(i, 2)
.List(.ListCount - 1, 1) = sh.Cells(i, 3)
.List(.ListCount - 1, 2) = sh.Cells(i, 4)
.List(.ListCount - 1, 3) = sh.Cells(i, 5)
.List(.ListCount - 1, 4) = sh.Cells(i, 6)
.List(.ListCount - 1, 5) = sh.Cells(i, 7)
End With
End If
Next x
Next i
'------------NSUTRADHAR--------------------------------
End Sub
Very nice, who gives code. Thnx for knowledge sharing....
ReplyDelete