Sep 8 2009

Working with Userforms Part 1: Hello World! Button

Over the next few posts we will look at how to build a userform and put it to good use.  Using a userform will provide additional functionality in your project that using the spreadsheet alone may not provide.  One way to give your project that customized polished look, may be to incorporate the use of userforms.


How To Add a Userform to your Project
There are many ways to achieve the next few steps I’m about to show you, but this is one way that I think is pretty straight forward.  In this example, we will add a userform to our project with a small button that will say “Hello World!” when clicked.  We will build upon this example in future posts.

  1. To access the VBA portion of Microsoft Excel just hit ALT+F11 while Excel is active (Hint: you can hit ALT+F11 to switch between the code editor and your spreadsheet at anytime to quickly switch windows). 
  2. From the file menu at the top, select Insert–>Userform.  A blank userform will appear.
  3. If the toolbox isn’t already displayed on your screen, hit View–>Toolbox and it will appear.
  4. Select the button icon from the toolbox and use it to draw you button onto the userform. 
  5. Change the text of the button by clicking on it and typing whatever you want the button to say.
  6. Now for the tricky part – to add the code telling the button what to do when clicked, double click on the button to access its “_Click” event code.  A window should open up with code that looks like this:

Private Sub CommandButton1_Click()

End Sub

  1. Now let’s add our message box to pop up with the text for hello world. Your vba should look like this:

Private Sub CommandButton1_Click()
MsgBox (“Hello World!”)
End Sub

  1. Now we need to add a button to click to call the userform we want to show. So add a button to your spreadsheet and change the text to “Userform Test”. (Add the “Forms” toolbar to your visible toolbars if you haven’t already; the buttons control is located on that toolbar)
  1. Hit ALT+F11 one more time to go back to your code. This time we want to insert a macro by selecting from the file menu: INSERT–>MODULE. An empty macro should appear.
  1.   Replace the macro with this code (more info on getting started with macros):

Sub TestMacro()
End Sub

  1.   Now go back to the button you added to the spreadsheet. Right-click and select “Assign Macro”. Select “TestMacro” from the list and click OK.
  1.   Finally, click off the button into a cell and then click your newly created button. You should see your userform pop up with a button you can click on. Click the button and a “Hello World!” message will appear something like the screenshot below. That’s It!!
Userform Example

Userform Example


Going Forward with Userforms
We just built a simple example showing how to use a userform. Although this is just the starting point, hopefully you can see the possibilities that adding this to your skillset can provide. Adding comboboxes, listboxes, textbox, etc. to your userform is just around the corner. The goal is to provide the best user experience as possible – and adding userforms can be a step in the right direction.

More Excel Tips

Did you find this post helpful? Try signing up for our free daily tips.

Enter your email address: 


Comments are closed.

Sign Up for Excel Hints

Free updates with Excel Hints, Tips & Tricks