XL: Creating a Dynamic List in a Custom Dialog Box

ID: Q129072

The information in this article applies to:

SUMMARY

In Microsoft Excel, it is possible to create a dynamic list or a list in a custom dialog box that changes based on some control chosen in the dialog box, such as a button. The following example shows how you can change the list that is displayed in a custom dialog box while the dialog box is still displayed.

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/

In this example, the dialog box that you create displays an empty list (List Box control) when you first run the dialog box. When you choose the List One button, a list of different kinds of fruit is displayed in the dialog box. When you choose the List Two button, a list of colors appears in the dialog box.

To Create the Custom Dialog Box

1. To add a new dialog sheet to your workbook, choose Macro from the Insert

   menu, and then choose Dialog.

2. Use the Forms toolbar to add a list box and two buttons to your dialog
   box.

3. Type the text you want to appear on the custom buttons, such as "List
   One" and "List Two" (without the quotation marks).

4. Select the list box. In the Name box, type "List" (without the quotation
   marks), and press ENTER.

5. Select one of the custom buttons (not OK or Cancel), and choose Object
   from the Format menu. In the Format Object dialog box, select the
   Control tab. Select the Dismiss check box, and then choose OK.

6. Repeat Step 4 with the second custom button.

To Enter Data on the Worksheet

1. On a worksheet in the workbook, enter the two different lists that you

   want displayed in the dialog box. For example, enter the following on
   Sheet1:

      A1: Apple    B1: Red
      A2: Orange   B2: White
      A3: Banana   B3: Blue
      A4: Pear     B4: Green
      A5: Grapes   B5: Yellow

To Create a Procedure to Run the Dialog Box

1. Insert a new module sheet in your workbook by choosing Macro from the

   Insert menu, and then choosing Module.

2. In the new module, enter the following:

      ' Dimension variables.
      Dim chosen As Integer, selected As Integer, MyList As Object
      Dim ListOneRange As String, ListTwoRange As String

      Sub Main()
         ' Set value of variable 'chosen' to 1.
         chosen = 1

         ' Assign value of variable 'MyList' to listbox in dialog box.
         Set MyList = Application.DialogSheets("Dialog1").ListBoxes("List")

         ' Define variable 'ListOneRange' as the cell range
         ' that contains your first list on the worksheet.
         ListOneRange = "Sheet1!$A$1:$A$5"

         ' Define variable 'ListTwoRange' as the cell range
         ' that contains your second list on the worksheet.
         ListTwoRange = "Sheet1!$B$1:$B$5"

         ' Initialize the list displayed in dialog box to be empty.
         MyList.ListFillRange = ""

         ' Loop to display the dialog box.
         ' Loop displays the dialog box until it is canceled.
         While chosen > 0

            show:

            ' Display the dialog box.
            DialogSheets("Dialog1").show

            ' If the value of 'chosen' is 1, the List One button was 
            ' chosen.
            If chosen = 1 Then

               ' Set range to first list on worksheet
               ' and display dialog box again.
               MyList.ListFillRange = ListOneRange
               GoTo show

            ' If the value of 'chosen' is 2, the List One button was 
            ' chosen.
            ElseIf chosen = 2 Then

               ' Set range to second list on worksheet
               ' and display dialog box again.
               MyList.ListFillRange = ListTwoRange
               GoTo show

            End If

         ' Repeat loop.
         Wend

      End Sub

      Sub OptionOne_Click()

         ' Set value of variable 'chosen' to 1.
         chosen = 1

      End Sub

      Sub OptionTwo_Click()

         ' Set value of variable 'chosen' to 2.
         chosen = 2

      End Sub

      Sub CancelChosen()

         ' Set value of variable 'chosen' to 0.
         chosen = 0

      End Sub

      Sub OKChosen()

         ' OK button was chosen.
         ' Set value of variable 'selected' to number
         ' corresponding to the item selected in the list.
         selected = MyList.ListIndex

         If selected = 0 Then

            ' Alert if no item is selected.
            MsgBox "nothing selected"

         Else

            ' Display selected item in message box.
            MsgBox MyList.List(selected)

         End If

      End Sub

3. Select the dialog sheet tab. On the dialog frame, select the OK button
   and choose Assign macro from the Tools menu. From the Macro
   Name/Reference list, select OKChosen, and choose OK.

4. Select the Cancel button and choose Assign macro from the Tools menu.
   From the Macro Name/Reference list, select CancelChosen, and choose OK.

5. Select the first custom button (List One in this example) and choose
   Assign macro from the Tools menu. From the Macro Name/Reference list,
   select OptionOne_Click, and choose OK.

6. Select the second custom button (List Two in this example) and choose
   Assign macro from the Tools menu. From the Macro Name/Reference list,
   select OptionTwo_Click, and choose OK.

7. To display the dialog box, choose Macro from the Tools menu. From the
   Macro Name/Reference list, select Main, and choose Run.

When you choose the List One button in the dialog box, the first list, the list of fruit is displayed. When you select List Two button, the second list, the list of colors, is displayed. When you choose OK, a dialog box appears with the item that you selected in the list. To close the custom dialog box, choose Cancel.

REFERENCES

For more information about the Control Properties Button or the List Box Button, choose the Search button in Help and type:

   custom dialogs, adding controls

Additional query words: 5.00 5.00a 5.00c howto
Keywords          : kbcode kbprg PgmCtrlsStd 
Version           : WINDOWS: 5.0,5.0c,7.0; MACINTOSH:5.0,5.0a
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto

Last Reviewed: May 17, 1999