?
Solved

Find listindex position in listbox

Posted on 2013-05-10
5
Medium Priority
?
923 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:Massimo Scola
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 49

Assisted Solution

by:Martin Liss
Martin Liss earned 200 total points
ID: 39157227
Private Sub cboCustomers_Click()
 txtList.Text = cboCustomers.ListIndex
End Sub

Open in new window

0
 

Author Comment

by:Massimo Scola
ID: 39157259
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 81

Expert Comment

by:byundt
ID: 39157295
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 81

Accepted Solution

by:
byundt earned 1800 total points
ID: 39157306
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:Massimo Scola
ID: 39159925
Hello Brad
Thanks a lot for your help.

Massimo
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

800 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