Article ID: 129072
Article Last Modified on 10/10/2006
A1: Apple B1: Red
A2: Orange B2: White
A3: Banana B3: Blue
A4: Pear B4: Green
A5: Grapes B5: Yellow
' 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
custom dialogs, adding controls
Additional query words: 5.00a 5.00c howto XL
Keywords: kbcode kbhowto kbprogramming KB129072