Writing Your First Macro
We will write and run our first simple Macro, just to have a feel of how it looks like.
What our first macro will do is simple. It will ask for your name, put the name in one of the Cells and change the color of that particular Cell to Green, all these 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).
- 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.7: Showing the VBA Editor Window
Fig 1.8 (a): Adding Command Button
Fig 1.8(b): 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
Fig 1.9 (a)
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
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 Macro. We are still going to talk extensively on all these and also on some other necessary things as you progress in this tutorial.
Chapter 2: Coming Soon