Tuesday, September 25, 2012

Excel VBA - Create a Macro in Excel 2007

Turn on the Developer Tab


1. Click on the Office button in the upper left corner of your screen.
2. Click on Excel Options. The Excel Options dialog box appears (see picture below).
3. Check Show Developer tab in the Ribbon.

Activate the Developer Tab in Excel 2007

4. Click OK.

Create a command button


You can now click on the Developer tab which has been placed next to the View tab.
1. Click on Insert.
2. Click on Command Button in the ActiveX Controls section.

Create a Command Button in Excel 2007

3. Now you can drag a command button on your worksheet.

Create and Assign the Macro


Now it is time to create a macro (a piece of code) and assign it to the command button.
1. Right click on CommandButton1.
2. Click on View Code.

Assign Code

3. The Visual Basic Editor appears. Place you cursor between 'Private Sub CommandButton1_Click()' and 'End Sub'.
4. For example, add the following code line:
Range("A1").Value = "Hello"

Create a Macro in Excel VBA

This macro places the word Hello into cell A1.
5. Close the Visual Basic Editor.
6. Before you click on CommandButton1, make sure Design Mode is deselected. You can do this by clicking on Design Mode again.
7. Click on CommandButton1.
Cell A1 should contain the word Hello now. Congratulations. You've just created a macro in Excel VBA!
Did you find this information helpful? Show your appreciation, follow me.

No comments:

Post a Comment