[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 937
  • Last Modified:

Find listindex position in listbox

I've got a question regarding listboxes. In this example, I've populated a listbox with a range (customers) from a worksheet.

How do I find the listindex position of a customer (column 2)?
I know that, for example "Robert" is (by looking at the listbox) in listindex position 2. Is there a way to do this programmatically?

Thanks

Massimo

screengrab of the userformlistbox.xlsm
0
Massimo Scola
Asked:
Massimo Scola
  • 2
  • 2
2 Solutions
 
Martin LissRetired ProgrammerCommented:
Private Sub cboCustomers_Click()
 txtList.Text = cboCustomers.ListIndex
End Sub

Open in new window

0
 
Massimo ScolaAuthor Commented:
Hello Martin
That works but I should have used another userform instead.

Let's assume the name needs to be searched in column 2 of the listbox, how do you do that? I've changed the userform a bit. new userform - with textbox instead of comboboxlistbox2.xlsm
0
 
byundtCommented:
For the first example, you could use this code to identify the ListIndex and select the name chosen in the combobox:
Private Sub cboCustomers_Change()
Dim i As Integer
With Me.lstCustomers
    For i = 1 To .ListCount
        If .Column(1, i - 1) = Me.cboCustomers.Value Then
            Me.txtList.Value = i - 1
            Exit For
        End If
    Next
End With
End Sub

Private Sub cmdClick_Click()
If txtList.Value <> "" Then Me.lstCustomers.Selected(txtList.Value) = True
End Sub

Open in new window

Brad
0
 
byundtCommented:
And for the second workbook, the code would be:
Private Sub txtName_Change()
Dim i As Integer
With Me.lstCustomers
    For i = 1 To .ListCount
        If .Column(1, i - 1) = txtName.Value Then
            Me.txtList.Value = i - 1
            Exit For
        End If
    Next
End With
End Sub

Private Sub cmdClick_Click()
If txtList.Value <> "" Then Me.lstCustomers.Selected(txtList.Value) = True
End Sub

Open in new window

0
 
Massimo ScolaAuthor Commented:
Hello Brad
Thanks a lot for your help.

Massimo
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now