Search value in textbox1 in the listbox1 with array doesn't work

david2005wang

New Member
Joined
Apr 8, 2022
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
Dear guys, I have codes as followings:
1. draw a userform with listbox1, and textbox1 which is used for input to search like-text in the listbox1 which shows the list of manufacturers in the sheets("manufacture").Range ("A1:A6"), the last row of the sheet , and 2 show the research result in the listbox1, but doens't work, it shows subscribe our of range in the line "brr(j, 1) = arr(i, 1)".
colud any expert to go through, check the code, and provide how to revise the code? thank you guys!

Private Sub UserForm_Initialize()
Dim sht As Worksheet, n As Long
Dim arr()
Set sht = Sheets("Manufacture")
n = sht.Range("A" & Rows.Count).End(xlUp).Row
arr = sht.Range("A1:A" & n)
ListBox1.List = arr
End Sub

Private Sub TextBox1_Change()
Dim sht As Worksheet, n As Long, arr(), C As Boolean
Set sht = Sheets("Manufacture")
n = sht.Range("A" & Rows.Count).End(xlUp).Row
arr = sht.Range("A1:A" & n)
Dim brr()
Dim i, j As Integer
For i = 1 To UBound(arr)
C = arr(i, 1) Like "*" & TextBox1.Text & "*"
If C Or i = 1 Then
j = j + 1
brr(j, 1) = arr(i, 1)
End If
Next
ListBox1.List = brr
End Sub
 

Attachments

  • Screenshot (77).png
    Screenshot (77).png
    27.9 KB · Views: 9

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this:

VBA Code:
Private Sub UserForm_Initialize()
  Dim sht As Worksheet, n As Long
  Dim arr()
  
  Set sht = Sheets("Manufacture")
  n = sht.Range("A" & Rows.Count).End(xlUp).Row
  arr = sht.Range("A1:A" & n)
  ListBox1.List = arr
End Sub

Private Sub TextBox1_Change()
  Dim sht As Worksheet
  Dim brr As Variant, arr As Variant
  Dim i As Long, j As Long, n As Long, m As Long
  
  Set sht = Sheets("Manufacture")
  n = sht.Range("A" & Rows.Count).End(xlUp).Row
  arr = sht.Range("A1:A" & n)
  
  m = WorksheetFunction.CountIf(sht.Range("A1:A" & n), "*" & TextBox1.Text & "*")
  ReDim brr(1 To m + 1, 1 To 1)
  For i = 1 To UBound(arr)
    If arr(i, 1) Like "*" & TextBox1.Text & "*" Or i = 1 Then
      j = j + 1
      brr(j, 1) = arr(i, 1)
    End If
  Next
  ListBox1.List = brr
End Sub
 
Upvote 0
Another way maybe

Rich (BB code):
Dim arr             As Variant
Private Sub UserForm_Initialize()
    Dim sht         As Worksheet
    Dim n           As Long
  
    Set sht = ThisWorkbook.Worksheets("Manufacture")
    n = sht.Range("A" & sht.Rows.Count).End(xlUp).Row
    arr = sht.Range("A2:A" & n)
    Me.ListBox1.List = arr
  
End Sub

Private Sub TextBox1_Change()
    Dim Search      As String
    Dim r           As Long
  
    Search = Me.TextBox1.Value
  
    With Me.ListBox1
        .Clear
        If Len(Search) > 0 Then
            For r = 1 To UBound(arr, 1)
                If UCase(arr(r, 1)) Like "*" & UCase(Search) & "*" Then
                    .AddItem arr(r, 1)
                End If
            Next
        Else
            .List = arr
        End If
    End With
  
End Sub

note variable arr this MUST sit at the very TOP of your userform code page OUTSIDE any procedure

Solution may not be as quick as the array version you were attempting but might be sufficient for your requirement.
Also, solution is not case sensitive & the list will reset when Textbox is cleared

Dave
 
Last edited:
Upvote 0
Solution
Another way maybe

Rich (BB code):
Dim arr             As Variant
Private Sub UserForm_Initialize()
    Dim sht         As Worksheet
    Dim n           As Long
 
    Set sht = ThisWorkbook.Worksheets("Manufacture")
    n = sht.Range("A" & sht.Rows.Count).End(xlUp).Row
    arr = sht.Range("A2:A" & n)
    Me.ListBox1.List = arr
 
End Sub

Private Sub TextBox1_Change()
    Dim Search      As String
    Dim r           As Long
 
    Search = Me.TextBox1.Value
 
    With Me.ListBox1
        .Clear
        If Len(Search) > 0 Then
            For r = 1 To UBound(arr, 1)
                If UCase(arr(r, 1)) Like "*" & UCase(Search) & "*" Then
                    .AddItem arr(r, 1)
                End If
            Next
        Else
            .List = arr
        End If
    End With
 
End Sub

note variable arr this MUST sit at the very TOP of your userform code page OUTSIDE any procedure

Solution may not be as quick as the array version you were attempting but might be sufficient for your requirement.
Also, solution is not case sensitive & the list will reset when Textbox is cleared

Dave

Another way maybe

Rich (BB code):
Dim arr             As Variant
Private Sub UserForm_Initialize()
    Dim sht         As Worksheet
    Dim n           As Long
 
    Set sht = ThisWorkbook.Worksheets("Manufacture")
    n = sht.Range("A" & sht.Rows.Count).End(xlUp).Row
    arr = sht.Range("A2:A" & n)
    Me.ListBox1.List = arr
 
End Sub

Private Sub TextBox1_Change()
    Dim Search      As String
    Dim r           As Long
 
    Search = Me.TextBox1.Value
 
    With Me.ListBox1
        .Clear
        If Len(Search) > 0 Then
            For r = 1 To UBound(arr, 1)
                If UCase(arr(r, 1)) Like "*" & UCase(Search) & "*" Then
                    .AddItem arr(r, 1)
                End If
            Next
        Else
            .List = arr
        End If
    End With
 
End Sub

note variable arr this MUST sit at the very TOP of your userform code page OUTSIDE any procedure

Solution may not be as quick as the array version you were attempting but might be sufficient for your requirement.
Also, solution is not case sensitive & the list will reset when Textbox is cleared

Dave
Dear dmt32, you are 100% correct, and your codes work well. one of my mistake probably is that the value to give to brr() is not right, I guess. Your codes also improve a lot technically from step to step in case of searching and showing in the listbox.
 
Upvote 0
ve to brr() is not
Try this:

VBA Code:
Private Sub UserForm_Initialize()
  Dim sht As Worksheet, n As Long
  Dim arr()
 
  Set sht = Sheets("Manufacture")
  n = sht.Range("A" & Rows.Count).End(xlUp).Row
  arr = sht.Range("A1:A" & n)
  ListBox1.List = arr
End Sub

Private Sub TextBox1_Change()
  Dim sht As Worksheet
  Dim brr As Variant, arr As Variant
  Dim i As Long, j As Long, n As Long, m As Long
 
  Set sht = Sheets("Manufacture")
  n = sht.Range("A" & Rows.Count).End(xlUp).Row
  arr = sht.Range("A1:A" & n)
 
  m = WorksheetFunction.CountIf(sht.Range("A1:A" & n), "*" & TextBox1.Text & "*")
  ReDim brr(1 To m + 1, 1 To 1)
  For i = 1 To UBound(arr)
    If arr(i, 1) Like "*" & TextBox1.Text & "*" Or i = 1 Then
      j = j + 1
      brr(j, 1) = arr(i, 1)
    End If
  Next
  ListBox1.List = brr
End Sub
Dear DanteAmor, your code is work, and correct my mistakes. thanking you!
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,033
Members
449,092
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top