Friday, September 9, 2011

Create a Macro in Excel 2007

Turn on the Developer Tab

1. Click on the File tab and choose Options. The Excel Options dialog box appears (see picture below).

2. Click Customize Ribbon on the left side of the dialog box.
3. Under Choose commands from on the left side of the dialog box, select Popular Commands (if necessary).
4. Under Customize the ribbon on the right side of the dialog box, select Main tabs (if necessary).
5. Check the Developer check box and click OK.
Activate the Developer Tab in Excel 2010

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
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 a Macro
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 the command button on the sheet, make sure Design Mode is deselected. You can do this by clicking on Design Mode again.
Result when you click the command button on the sheet:
Excel Macro Result
Congratulations. You've just created a macro in Excel VBA!

 For 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!

Create a Macro in Excel 2003

Activate the Control Toolbox

In order to use Excel VBA in Excel 2003, you have to activate the Control Toolbox.
1. Click on View, Toolbars, Control Toolbox.

Activate the Control Toolbox in Excel 2003
2. You can integrate the Control Toolbox if you want by simply dragging it into your menu.
Integrate the Control Toolbox

Create a Command Button

1. Click on Command Button from the Control Toolbox.

Click on Command Button
2. Now you can drag a command button on your worksheet.

Create and Assign the Macro

Now it is time to create a macro and assign it to the command button.
1. Right click on CommandButton1.
2. Click on View Code.
Right Click and then click on View Code
3. The Visual Basic Editor appears. Place you cursor between 'Private Sub CommandButton1_Click()' and 'End Sub'.

4. Add the line:
Range("A1").Value = "Hello"

The Visual Basic Editor in Excel 2003
5. Close the Visual Basic Editor.
6. Before you click on the command button, make sure Design Mode (most left field of the Control Toolbox) is deselected. You can do this by clicking on Design Mode again.
7. Click on CommandButton1.
Result:
First Macro Result
Well done! You've just created a macro in Excel VBA!

No comments:

Post a Comment