These steps are done using Office 2010 (except where noted otherwise) but should be similar enough for 2007 and 2013 as well. I'm doing it with Excel but the differences should be subtle.
The first step is to add the Developer tab to the ribbon.
In Office 2007,
Click “Office Menu” -> “(Program) Options”.
(“Office Menu” refers to the button in the top left corner.)
This brings up a menu that has an “Excel Options”, “Word Options”, etc., button at the bottom.
In the options dialog box, check “Show Developer tab in the Ribbon”:
and click “OK”.
- Your new “Developer” tab should be showing.
In Office 2010 and 2013,
Click File -> Options.
Choose Customize Ribbon.
On the right side, find Developer and check the box. Click OK.
Your new tab should be showing, click on it and then click on Visual Basic.
A daunting screen is shown, which is the VBA code editing window.
Side note: You can also open the VBA code editing window by using the Alt+F11 keyboard shortcut (even if the Developer tab is not showing).
Inside the code editing window, on the left side you should have a pane called Project-VBAProject. In this, under the Microsoft Excel Objects (this name is different depending on which program (Word, Outlook, etc.) you are using) is a 'thing' called ThisWorkbook. Double click on ThisWorkbook
In this example, we'll use the following code.
sub WarnMeOfDanger()
MsgBox "Argh, a dragon is behind you!"
end sub
All the above does is show open a dialog box to show a message on-screen.
Copy the code, and paste it into the white sheet in the VBA code editing window.
Now, at the top of the VBA code editor window, click the green play button and you should see a message.
So, every time you want to run the code, return to this screen and press play!
However, you may prefer to add your own button. If you want to add a button then, first, note the first line of the code has WarnMeOfDanger()
. This is actually the name of our 'subroutine'. We'll remember this for later. Now, simply close the Window you've been using (the code is automatically saved).
Still in the Developer ribbon, click on Insert, and choose the first item, which is a button. Your mouse cursor becomes a little cross (not angry, I mean its appearance). Draw the size of the button. A new window appears, and it should show us the option to assign the button to a function. In this case, we should see something like ThisWorkbook.WarnMeOfDanger
. Click on this and click OK.
Now, if you press your new button, the message will show. Right-click the button to get options to move it, change the text, resize etc.