Design the User Form
With your macro-enabled spreadsheet open, click on the Developer ribbon at the top of Excel. Click on Visual Basic
in the Coding
panel to open up the VBA Editor.
When your editor opens, click on Insert > User Form
from the menu bar at the top.
In the properties panel for the new form, change the Name
property to Nations
. Locate the Caption
property and change it to Nations
.
In the VBA toolbox locate the TreeView control:
Draw a TreeView onto your form. Draw it on the left, and then use the resizing handles to make it a decent height. Make it wide enough for some text, but not too wide.
With your TreeView selected, change the following two properties:
LineStyle
: tvwRootLines
Style
: tvwTreeLinesPlusMinusText
Your TreeView should look like this when you're done:
Notice that the default parent and child nodes just say, "Sample Node". We'll change these defaults with code.
Add 5 labels to the right of your TreeView. Change the Caption
properties to: Populations (in millions), Life expectancy, Currency, Capital, and Fun Facts. Add four more labels to the right of the first four labels. You can change the style of your labels. Experiment with the following properties of labels: BorderStyle, BorderColor, BackColor, Font.
Now add a textbox just below the Fun Facts label. Just below the textbox, add a Command Button
. Change the Caption
property to Exit
.
Your form in design view should look something like ours below:
Before getting into the TreeView code, what we can do is to add the code for the Command Button. Double click your Exit button to open up its code stub. Now add the following line:
The Me keyword refers to the current form. When this line is executed, the form will close down.