XL98: How to Populate One ListBox Based on Another ListBox (182492)



The information in this article applies to:

  • Microsoft Excel 98 Macintosh Edition

This article was previously published under Q182492

SUMMARY

This article contains an example that uses the selected item in one ListBox control on a UserForm to determine the list that populates a second ListBox control.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To use this example, follow these steps:

  1. Close and save any open workbooks, and then open a new workbook.
  2. On Sheet1, enter the following values:
         A1: North Carolina  B1: Charlotte  C1: Charleston  D1: Charlottesville
         A2: South Carolina  B2: Greensboro C2: Columbia    D2: Norfolk
         A3: Virginia        B3: Raleigh    C3: Greenville  D3: Richmond
    						
  3. Start the Visual Basic Editor (press OPTION+F11).
  4. On the Insert menu, click UserForm.
  5. Draw two ListBox controls on the UserForm.
  6. Draw another ListBox control on the UserForm
  7. Double-click the UserForm to display the Code window for the UserForm.
  8. In the module, type the following code:
           Private Sub UserForm_Initialize
    
              ' Populate ListBox1.
              ListBox1.List = Worksheets("Sheet1").Range("A1:A3").Value
    
           End Sub
    
    
           Private Sub ListBox1_Click()
    
               ' Get the currently selected item
               Select Case ListBox1.Value
    
                   ' If North Carolina, set List property of ListBox2
                   ' to Column B.
                   Case "North Carolina"
                       ListBox2.List = _
                           Worksheets("Sheet1").Range("B1:B3").Value
    
                   ' If South Carolina, set List property of ListBox2
                   ' to Column C.
                   Case "South Carolina"
                       ListBox2.List = _
                           Worksheets("Sheet1").Range("C1:C3").Value
    
                   ' If Virginia, set List property of ListBox2 to
                   ' Column D.
                   Case "Virginia"
                       ListBox2.List = _
                           Worksheets("Sheet1").Range("D1:D3").Value
    
               End Select
    
           End Sub
    						
  9. Run the UserForm.

    When you choose one of the items in ListBox1, the list in ListBox2 is updated, reflecting the choice you made in ListBox1.
  10. Close the UserForm.

REFERENCES

For more information about list boxes, click the Office Assistant in the Visual Basic Editor, type listbox, click Search, and then click to view "ListBox control."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

179216 OFF98: How to Use the Microsoft Office Installer Program


Modification Type:MajorLast Reviewed:6/17/2005
Keywords:kbcode kbhowto kbProgramming KB182492