XL98: How to Populate One ListBox Based on Another ListBox

ID: Q182492

The information in this article applies to:

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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

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:

   ARTICLE-ID: Q179216
   TITLE     : OFF98: How to Use the Microsoft Office Installer Program

Additional query words: 8.00 XL98 dynamic
Keywords          : kbcode kbprg xlvbahowto 
Version           : MACINTOSH:98
Platform          : MACINTOSH
Issue type        : kbhowto

Last Reviewed: May 18, 1999