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.
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.
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 add a new dialog sheet to your workbook, choose Macro from the Insert
menu, and then choose Dialog.
Use the Forms toolbar to add a list box and two buttons to your dialog
box.
Type the text you want to appear on the custom buttons, such as List
One and List Two.
Select the list box. In the Name box, type List, and press ENTER.
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.
Insert a new module sheet in your workbook by choosing Macro from the
Insert menu, and then choosing Module.
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
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.
Select the Cancel button and choose Assign macro from the Tools menu.
From the Macro Name/Reference list, select CancelChosen, and choose OK.
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.
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.
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.
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.