Learning to write your first macro in excel is very exciting. In this tutorial we will write and run our first simple Macro. This tutorial will be a great introduction to more complicated applications.

Our Macro will perform a simple task. It will ask for your name in a popup form and will take that name an input it into a cell. We will also change that Cell to Green. This is all down at the press of a single button.

So let’s start;

  • Open the VBA Editor (by clicking the Visual Basic on the Code panel of the Developer tab).
VBA Open
FIG 1.7 – Showing the VBA editor window

Add a Command Button: To do this, click on the Developer tab, click on Insert and from the ActiveX control group, select Command Button. Then drag the Command Button on your work sheet. FIG 1.8a, FIG 1.8b

Developer Tab
FIG 1.8a – Adding a command button
VBA Code
FIG 1.8b – Showing the ActiveX Control

Writing the Macros

Now, we will write the macros (codes) that would be executed whenever we click the Command Button that we just added. To do this, double click on the Command Button, the Editor Window will open. Place your cursor in between Private Sub and End Sub and write the lines of codes (macros) given in Figure 1.9 into that space.

VBA Code
FIG 1.9a
VBA Code
FIG 1.9b

Now, minimize or close the VBA Editor Window, Click on the Command Button on the Sheet. Then you see the output of our code.

Your results should look like this:

FIG 1.10 – Showing the result

And this;

VBA Code
FIG 1.11

Bravo! You’ve just written your first Macro. However, if you do not understand all the codes we’ve written, how it works and also the terms we used, DO NOT PANIC, as this is just to introduce you to writing a Macro.