Professional Documents
Culture Documents
html
You can change the names and the captions of the controls. Names are used in the
Excel VBA code. Captions are those that appear on your screen. It is good practice
to change the names of the controls, but it is not necessary here because we only
have a few controls in this example. To change the caption of the Userform and
command button, click View, Properties Window and click on each control.
5. To show the Userform, place a command button on your worksheet and add the
following code line:
UserForm1.Show
End Sub
We are now going to create the Sub UserForm_Initialize. When you use the Show
method for the Userform, this sub will automatically be executed.
7. In the Project Explorer, right click on UserForm1 and then click View Code.
8. Choose Userform from the left drop-down list. Choose Initialize from the right
drop-down list.
With ComboBox1
.AddItem "Animals"
.AddItem "Sports"
.AddItem "Food"
End With
End Sub
Explanation: These code lines fill the first combo box.
We have now created the first part of the Userform. Although it looks neat already,
nothing will happen yet when we select an item from the first combo box.
ComboBox2.Clear
End Sub
Explanation: Excel VBA uses the value of the variable index to test each subsequent
Case statement to see with which items the second combo box should be filled. Go
through our Select Case program to learn more about the Select Case structure.
Range("A1").Value = ComboBox2.Value
End Sub
Result: