Presentation on theme: "By Angel Aguila. Apply the range name Category to cells A1:C1, as you normally would. When lists are subject to updates, use a dynamic range instead."— Presentation transcript:
By Angel Aguila
Apply the range name Category to cells A1:C1, as you normally would. When lists are subject to updates, use a dynamic range instead. To create a dynamic range name for the Dairy list, do the following: Figure B
The next step is to display a list of categories in this first control. Well do so using the Category range (step 2). Double-click the control and enter the following procedure: Private Sub Worksheet_Activate() 'Populate combo box with inventory categories. Dim rng As Range Dim ws As Worksheet Set ws = Worksheets("Lists") Me.cboCategoryList.Clear For Each rng In ws.Range("Category") Me.cboCategoryList.AddItem rng.Value Next rng End Sub When youre done, return to the worksheet. We used VBA because the controls ListFillRange property requires a columnar list.
5: Use the first control Before using the control, click Design Mode in the Controls group. Look for Exit Design Mode in the Controls Toolbox in Excel You can use the first controls list by clicking its drop-down arrow and choosing any of the three categories, as shown in Figure C. Figure C
At this point, you have a static control thats displaying three items: Dairy, Produce, and Other. The only way to update this list is to add an item at the sheet level and update the Category range. (Youll see the significance of this in step 10.) Now youre ready to add the dependent list the one that changes depending on the selection in the first control. Using the instructions in step 3, insert a second combo box and name it cboDependentList.
The second control will display three lists based on the dynamic ranges you created in step 2 (Dairy, Produce, and Other). To program it, double-click the embedded control and enter the following procedure: Private Sub cboCategoryList_Change() Populate dependent combo box with appropriate list items 'according to selection in cboCategoryList. Dim rng As Range Dim ws As Worksheet Dim str As String Set ws = Worksheets("Lists") str = cboCategoryList.Value Me.cboDependentList.Clear On Error Resume Next For Each rng In ws.Range(str) Me.cboDependentList.AddItem rng.Value Next rng End Sub When youre done, return to the worksheet.
Choosing Produce from the first list populates the second list with a list of produce. Try it again. This time, choose Dairy from the first list. As you can see in Figure E, the list now displays the dairy items in column A (on the Lists sheet). This second procedure is similar to the first, but the first controls Change event executes this event, which in turn populates the dependent list. Both controls are ready to work together, so click Design Mode in the Controls group (or Exit Design Mode in the Controls Toolbox). Then, choose an item, such as Produce, from the first control. Click the second controls drop- down to view the list of produce items (from column B in the Lists sheet), as shown in Figure D. Figure D Figure E Choosing Dairy displays these items.
9: Update a list You just saw the benefits of a dependent list control. Now lets see what happens when you update one of the lists. Move to the Lists sheet and add Cornbread mix to the Other list, as shown in Figure F. Figure F Add a new item to the Other list.
10: See the dynamic results After adding an item to the Other list, return to the ListControls sheet and choose Other from the first control. Because we used dynamic ranges in step 2, the range automatically adjusts to include the new item, as shown in Figure G. Figure G