select item in listbox

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I'm using this code to insert the name in a textbox when I selected an item in a list box. Is it possible to type in a name in a textbox and and search and select the item listed in the listbox. I have 4 columns in the listbox and the names are listed in column(1)

Code:
cbName = ListBox1.Column(1, ListBox1.ListIndex)
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try...

Code:
    Dim name As String
    name = TextBox1.Value
    
    Dim i As Long
    With ListBox1
        .ListIndex = -1
        For i = 0 To .ListCount - 1
            If UCase(.Column(1, i)) = UCase(name) Then
                .ListIndex = i
                Exit For
            End If
        Next i
    End With

Hope this helps!
 
Upvote 0
Another way to select:

Code:
Private Sub CommandButton1_Click()
  Dim i As Long
  For i = 1 To ListBox1.ListCount - 1
    If LCase(ListBox1.List(i)) = LCase(TextBox1) Then ListBox1.Selected(i) = True
  Next

End Sub
 
Upvote 0
Hi Dante, I tried your code, but nothing was selected
 
Upvote 0
Thats weird!
Works for me!
Try this:

Code:
Private Sub CommandButton2_Click()
  Dim i As Long
  For i = 1 To ListBox1.ListCount - 1
    If LCase(ListBox1.List(i)) = LCase(TextBox1.Value) Then
      ListBox1.Selected(i) = True
      Exit For
    End If
  Next
End Sub
 
Upvote 0
Hi Dante,

Maybe you missed it, but the names are in the second column of the listbox, not the first one. Also, as I'm sure you know, the index for a listbox starts at 0, not 1. And, lastly, you might want to deal with situations where an item is already selected before the macro runs and the name doesn't exist in the listbox. In my example, if a name doesn't exist in the listbox, it de-selects any already selected item.

Cheers!
 
Last edited:
Upvote 0
Hi Dante,

Maybe you missed it, but the names are in the second column of the listbox, not the first one. Also, as I'm sure you know, the index for a listbox starts at 0, not 1. And, lastly, you might want to deal with situations where an item is already selected before the macro runs and the name doesn't exist in the listbox. In my example, if a name doesn't exist in the listbox, it de-selects any already selected item.

Cheers!

Thanks Domenic, you're right, typo error, it must start at 0.
But the OP said: "I have 4 columns in the listbox and the names are listed in column (1)"

That's why he doesn't find it, so it should be like this:

Code:
Private Sub CommandButton1_Click()
  Dim i As Long
  ListBox1.ListIndex = -1
  For i = 0 To ListBox1.ListCount - 1
    If LCase(ListBox1.List(i, [B][COLOR=#0000cd]1[/COLOR][/B])) = LCase(TextBox1) Then ListBox1.Selected(i) = True
  Next
End Sub
 
Upvote 0
Works great Dante! Thank you both for the help..Cheers
 
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,784
Members
449,049
Latest member
greyangel23

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