Multicolumn Listbox

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,060
I would like a script that would find all values in column A based on the value typed in textbox1 and then would put the cell value in column 1 of listbox1 and the cell address in column 2 of listbox1.

Can someone please help.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
sorry, just to clarify, the search would be a partial search match for values in column A.

thanks
 
Upvote 0
Surely there is an expert that can help, I have seen similar help but I can not modify the code as I have very basic vba skills.
 
Upvote 0
Code:
Dim SearchTerm as String
Dim dataRange as Range ,OneCell as Range

With ThisWorkbook.Sheets(1).Range("A:A")
    Set dataRange = Range(.Cells(1,1), .Cells(.Rows.Count,1).End(xlup))
End With

SearchTerm = TextBox1.Text

For Each oneCell in dataRange
    If CStr(oneCell.Value) = searchTerm Then
        With ListBox1
            .AddItem searchTerm
            .List(.ListCount - 1, 1) = CStr(oneCell.Offset(0,1).Value)
        End With
Next oneCell
 
Upvote 0
Hi Mike,

Thanks for helping me however I have a couple of problems with the script.

It is only matching a whole value, but I need it to match partial values.

It is only putting the cell value in the listbox, whereas it should be a multicolumn listbox with the value in the first column and then the cell address in the second column.

Thanks
 
Upvote 0
Code:
'...

For Each oneCell in dataRange
    If CStr(oneCell.Value) Like "*" & searchTerm & "*" Then
        With ListBox1
            .AddItem searchTerm
            .List(.ListCount - 1, 1) = .Address(False,False,xlA1,True)
        End With
Next oneCell
I don't understand how I misread the OP. This will find "Macy's catalog" if the search term is "cat" and will put the address in the second column.
 
Upvote 0
Mike,

I tested the script but it cliches on the address line

Rich (BB code):
.List(.ListCount - 1, 1) = .Address(False, False, xlA1, True)
 
Upvote 0
Gakk. I forgot which With contruct I was in. Also, the previous line should be altered to show the value found, rather than just the search term.
Code:
.AddItem CStr(oneCell.Value)
.List(.ListCount - 1, 1) = oneCell.Address(False,False,xlA1,True)
 
Upvote 0
Mike,

I feel bad but it still isn't adding the address into column 2 of the listbox and if as an example in column A of my spreadsheet I put values of jay1 down to jay10 and the search finds the value but only puts the name jay in the listbox not the actually cell value i.e. jay1 or jay2 etc...
 
Upvote 0
Did you try the two line edit shown in post #8, that should clear up the "Jay" vs "Jay3" issue.

My code assumes that the .ColumnCount property of the list box has already been set to 2.

That might explain why the address is not there.

I'm wondering, why do you need the address? If you are using it as a way to find your way back to the selected found term, you might want to look at the .ColumnWidths property set to ";0", (to hide that column from the user, if that's the look you want) and using the .BoundColumn and .TextColumn properties to make retrieving the selected address easier.
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,920
Members
448,533
Latest member
thietbibeboiwasaco

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