Solved

Find listindex position in listbox

Posted on 2013-05-10
5
866 Views
Last Modified: 2013-05-12
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
Comment
Question by:mscola
  • 2
  • 2
5 Comments
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 50 total points
Comment Utility
Private Sub cboCustomers_Click()
 txtList.Text = cboCustomers.ListIndex
End Sub

Open in new window

0
 

Author Comment

by:mscola
Comment Utility
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
 
LVL 80

Expert Comment

by:byundt
Comment Utility
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
 
LVL 80

Accepted Solution

by:
byundt earned 450 total points
Comment Utility
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
 

Author Closing Comment

by:mscola
Comment Utility
Hello Brad
Thanks a lot for your help.

Massimo
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Outlook Free & Paid Tools
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now