I have the following bit of code (gathered from various resources) that almost fulfills what I need, but, is missing a crucial 'search' element.
Private Sub CommandButton1_Click()
' Clear Listbox of last search results
Me.ListBox1.Clear
Dim lb As MSForms.ListBox
Dim srchArray() As Variant
Dim lrw As Long, lcol As Long
Dim rngTarget As Range
SearchString = InputBox("Enter any of the following criteria to complete your search", "Search")
Set rngTarget = Worksheets("Sheet1").Range("b5:h900")
ReDim Preserve srchArray(1 To rngTarget.Rows.count, 1 To rngTarget.Columns.count)
With rngTarget
For lcol = 1 To .Columns.count
For lrw = 1 To .Rows.count
' Do search here? Add results to array srchArray....
srchArray(lrw, lcol) = rngTarget.Cells(lrw, lcol)
Next lrw
Next lcol
End With
Set lb = Me.ListBox1
With lb
.ColumnCount = 7
.ColumnWidths = "100;100;30;300;30;80;100"
.List = srchArray
End With
End Sub
Which works, sorta, just not completely.
A little backstory: months ago, the data required to be returned to the listbox was just one column, so the search that I was using...
Private Sub CommandButton1_Click()
Me.ListBox1.Clear
SearchString = InputBox("Enter any of the following criteria to complete your search, "Search")
For Each d In Range("b6:g900")
If InStr(LCase(CStr(d)), LCase(SearchString)) Then
With ListBox1
.AddItem Cells(d.Row, 2) & " - " & Cells(d.row, 3) 'etc.
End With
End If
Next d
End Sub
worked great until more columns were added and the corresponding data had to be returned as well. Which doesn't mean the above didn't still work, it did, I just had to concatenate the other columns to each other. But, the problem was that none of the columns lined up properly, nor could I get the column headers like I wanted. So, I switched to the first method using an array to build my listbox - giving me greater control of the column widths (where I had none) and the headers as well.
But, I can't quite figure out how to iterate through the defined range, search for the desired text and then add that result to the array so that I can build my listbox.
I've tried several iterations of adding the search function to the second bit of code, but, no luck; I either get NO results, or a result containing everything within defined range.
Any help is appreciated. My google-fu is quite lacking today.
Thanks in advance. If you need more information, glad to provide.
J
Bookmarks