Tuesday, September 25, 2012

Excel VBA - Create a Macro in Excel 2010

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!
Did you find this information helpful? Show your appreciation, follow me.

No comments:

Post a Comment