Multicolumn Listbox

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,056
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.
 

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,056
sorry, just to clarify, the search would be a partial search match for values in column A.

thanks
 

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,056
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.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,784
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
 

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,056
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
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,784
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.
 

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,056
Mike,

I tested the script but it cliches on the address line

Rich (BB code):
.List(.ListCount - 1, 1) = .Address(False, False, xlA1, True)
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,784
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)
 

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,056
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...
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,784
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.
 

Forum statistics

Threads
1,081,693
Messages
5,360,668
Members
400,592
Latest member
katekoz

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top