Microsoft Knowledge Base Email Alertz

This article contains an example of using the selected item in one ListBox control on a UserForm to determine the list that will populate a second ListBox control.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 161518 - Last Review: November 23, 2006 - Revision: 2.4

XL97: How to Populate One ListBox Based On Another ListBox

This article was previously published under Q161518

SUMMARY

This article contains an example of using the selected item in one ListBox control on a UserForm to determine the list that will populate 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 a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104)

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (http://support.microsoft.com/gp/advisoryservice)

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms)
  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 ALT+F11).
  4. If the Properties Window is not visible, click Properties on the View menu (or press F4).
  5. On the Insert menu, click UserForm.
  6. Draw a ListBox control on the UserForm.
  7. Activate the Properties window (press F4).
  8. Select the RowSource property and type Sheet1!A1:A3.
  9. Draw another ListBox control on the UserForm
  10. Double-click the ListBox1 control (from Step 6) to open up the Code Window for the UserForm.
  11. In the module type the following code for the ListBox1 Click event:
    Private Sub ListBox1_Click()
    
               'Get the currently selected item
               Select Case ListBox1.Value
    
                   'If North Carolina, set RowSource property of ListBox2
                   'to Column B.
                   Case "North Carolina"
                       ListBox2.RowSource = "Sheet1!B1:B3"
    
                   'If South Carolina, set RowSource property of ListBox2
                   'to Column C.
                   Case "South Carolina"
                       ListBox2.RowSource = "Sheet1!C1:C3"
    
                   'If Virginia, set RowSource property of ListBox2 to
                   'Column D.
                   Case "Virginia"
                       ListBox2.RowSource = "Sheet1!D1:D3"
    
               End Select
    
           End Sub
    						
  12. Run the UserForm.

    When you choose one of the items in ListBox1, the list in ListBox2 updates, reflecting the choice you made in ListBox1.
  13. Close the UserForm.

REFERENCES

For more information about ListBoxes, click the Index tab in Visual Basic for Applications Help, type the following text
listbox
and then double-click the selected text to go to the "ListBox control" topic.

APPLIES TO
  • Microsoft Excel 97 Standard Edition
Keywords: 
kbdtacode kbprogramming KB161518
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate