Excel

Click Name in Listbox to Select Data in Sheet

Ease of Use

Intermediate

Version tested with

2000 

Submitted by:

lucas

Description:

Selects the row containing all of the data corresponding to a name clicked in a listbox 

Discussion:

A listbox showing just the pertinant information that you select will help you find what your looking for if you have large lists of names and associated data. 

Code:

instructions for use

			

Place this code In a standard module: Option Explicit Sub ListBoxForm() UserForm1.Show End Sub Place this code In the module For your userform: Option Explicit 'Populate a multi-column combo and list box with values from spreadsheet 'when the form is loaded Private Sub UserForm_activate() Dim MyList(10, 3) 'as array type Dim R As Integer 'The list box contains 3 data columns. 'You can configure the number of columns, their width and height below 'as well as change the text in the ControlTipText of the listbox Application.ShowToolTips = True With ListBox1 .ColumnCount = 3 .ColumnWidths = 75 .Width = 230 .Height = 110 .ControlTipText = "Click the Name, Job, or ID you're after" End With 'Define the list and where it's obtained from (Columns A, D, G in this example) With ActiveSheet 'MyList (Row{0 to 9}, Column{0 to 2}) = the ranges given For R = 0 To 9 MyList(R, 0) = .Range("A" & R + 1) MyList(R, 1) = .Range("D" & R + 1) MyList(R, 2) = .Range("G" & R + 1) Next R End With 'populate the list box ListBox1.List = MyList End Sub 'When a name in the listbox is clicked, select the corresponding row Private Sub listBox1_Click() Dim Employee As Variant Dim Name As String Dim firstaddress As String Employee = Empty 'If you add more than 500 names you will need to increase this With ActiveSheet.Range("a1:a500") Name = ListBox1.Value Set Employee = .Find(what:=Name, LookIn:=xlValues) If Not Employee Is Nothing Then Employee.Rows.EntireRow.Select Else Exit Sub End With 'closes the form when you click on a name Unload Me Set Employee = Nothing End Sub

How to use:

  1. Open the Visual Basic Editor by going to tools-Macro's-Visual Basic Editor or use Alt-F11
  2. On the toolbar of the Visual Basic Editor, go to insert - form
  3. It needs to be named UserForm1 or be changed in the code to match what you use to show the form in the standard module.
  4. Onto your form drag one listbox from the toolbox.
  5. It should be named ListBox1 by default, if not rename it or change the name in the code to match from ListBox1 to the name of your listbox.
  6. In the project explorer right click on the form you have created and left click on view code.
  7. In the module pane for the form paste the code above.
  8. Go to the main menu and click on insert-module.
  9. Into this code module paste the code above for the standard module.
  10. Close the Visual Basic Editor by clicking the X in the upper right corner or go to File-Close
 

Test the code:

  1. On the main menu go to tools-macro-macros.
  2. In the dialog window select ListBoxForm and then click run.
  3. When the Listbox appears just click on a name and the row containing their data will be selected.
 

Sample File:

list_box_select.zip 14.36KB 

Approved by mdmackillop


This entry has been viewed 474 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express