Microsoft KB Archive/182492

= XL98: How to Populate One ListBox Based on Another ListBox =

Article ID: 182492

Article Last Modified on 6/17/2005

-

APPLIES TO


 * Microsoft Excel 98 for Macintosh

-



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:

 Close and save any open workbooks, and then open a new workbook.  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  Start the Visual Basic Editor (press OPTION+F11). On the Insert menu, click UserForm. Draw two ListBox controls on the UserForm. Draw another ListBox control on the UserForm</li> Double-click the UserForm to display the Code window for the UserForm.</li>  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 </li> 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.</li> Close the UserForm.</li></ol>

<div class="references_section">