macro 2
macro 2
With Excel VBA you can automate tasks in Excel by writing so-called macros. In this
chapter, learn how to create a simple macro which will be executed after clicking on a
command button. First, turn on the Developer tab.
Developer Tab
1. Right click anywhere on the ribbon, and then click Customize the Ribbon.
2. Under Customize the Ribbon, on the right side of the dialog box, select Main tabs (if
necessary).
5. You can find the Developer tab next to the View tab.
Command Button
Assign a Macro
To assign a macro (one or more code lines) to the command button, execute the following
steps.
3. Place your cursor between Private Sub CommandButton1_Click() and End Sub.
6. Click the command button on the sheet (make sure Design Mode is deselected).
Result:
To open the Visual Basic Editor, on the Developer tab, click Visual Basic.
This example teaches you how to swap two values in Excel VBA. You will often need this
structure in more complicated programs as we will see later.
Situation:
Place a command button on your worksheet and add the following code lines:
temp = Range("A1").Value
3. Now we can safely write the value of cell B1 to cell A1 (we have stored the value of cell
A1 to temp so we will not lose it).
Range("A1").Value = Range("B1").Value
4. Finally, we write the value of cell A1 (written to temp) to cell B1.
Range("B1").Value = temp
Result:
As a beginner to Excel VBA, you might find it difficult to decide where to put your VBA
code. The Create a Macro chapter illustrates how to run code by clicking on a command
button. This example teaches you how to run code from a module.
Sub Cyan()
End Sub
Note: a procedure is either a sub or a function. Learn more about functions and subs here, if
you like.
4. The sub changes the background color of your worksheet to cyan. To achieve this, add the
following code line:
Cells.Interior.ColorIndex = 28
Note: instead of ColorIndex number 28 (cyan), you can use any ColorIndex number.
5. Click Macros.
6. Select Cyan and click Run.
Result:
Note: code placed into a module is available to the whole workbook. That means you can
select Sheet2 or Sheet3 and change the background color of these sheets as well. The Add a
Macro to the Toolbar program illustrates how to make a macro available to all your
workbooks (Excel files). Remember, code placed on a sheet (assigned to a command button)
is only available for that particular sheet.
The Macro Recorder, a very useful tool included in Excel VBA, records every task you
perform with Excel. All you have to do is record a specific task once. Next, you can execute
the task over and over with the click of a button. The Macro Recorder is also a great help
when you don't know how to program a specific task in Excel VBA. Simply open the Visual
Basic Editor after recording the task to see how it can be programmed.
Record a Macro
3. Select This Workbook from the drop-down list. As a result, the macro will only be
available in the current workbook.
Note: if you store your macro in Personal Macro Workbook, the macro will be available to all
your workbooks (Excel files). This is possible because Excel stores your macro in a hidden
workbook that opens automatically when Excel starts. If you store your macro in New
Workbook, the macro will only be available in a new workbook which Excel opens
automatically for you.
4. Click OK.
5. Right mouse click on the active cell (selected cell). Be sure not to select any other cell!
Next, click Format Cells.
6. Select Percentage.
7. Click OK.
8. Finally, click Stop Recording.
Now we'll test the macro to see if it can change the number format to Percentage.
Result:
See the Macro
Note: the macro has been placed into a module called Module1. Code placed into a module is
available to the whole workbook. That means you can change the number format of cells on
other sheets as well. Remember, code placed on a sheet (assigned to a command button) is
only available for that particular sheet. You can ignore the Option Explicit statement for now.
Unfortunately, there are a lot of things you cannot do with the Macro Recorder. For example,
you cannot loop through a range of data with the Macro Recorder. Moreover, the Macro
Recorder uses a lot more code than is required, which can slow your process down.
Result:
6. Empty Range("B3:B5").
7. Select any cell on the sheet and run the recorded macro.
Result:
A macro recorded in absolute mode always produces the same result.
Wouldn't it be nice to place these words anywhere on the sheet automatically? Not just
Range("B3:B5"). This would make the macro much more flexible. Solution: record the macro
in relative mode.
Result:
7. Click Stop Recording.
8. Select any other cell (for example, cell D4) and run the recorded macro.
Result:
Excel places the words relative to the initial selected cell. That's why it's called recording in
relative mode.
This example illustrates the difference between A1, R1C1 and R[1]C[1] style in Excel VBA.
1. Place a command button on your worksheet and add the following code line (A1 style):
Range("D4").Formula = "=B3*10"
Result:
Range("D4").FormulaR1C1 = "=R3C2*10"
Result:
Explanation: cell D4 references cell B3 (row 3, column 2). This is an absolute reference ($
symbol in front of the row number and column letter).
Range("D4").FormulaR1C1 = "=R[-1]C[-2]*10"
Result:
Explanation: cell D4 references cell B3 (one row above and 2 columns to the left). This is a
relative reference. This code line gives the exact same result as the code line used at step 1.
4. Why learning about this? Because the Macro Recorder uses the FormulaR1C1 property
(R[1]C[1] style). The Macro Recorder creates the following code lines if you enter the
formula =B3*10 into cell D4.
Explanation: you can see that this is the exact same code line used at step 3.
If you use an Excel macro frequently, you can add it to the Quick Access Toolbar. This
way you can quickly access your macro. First, we record an empty macro.
2. Name the macro MyName. Choose to store the macro in Personal Macro Workbook. This
way the macro will be available to all your workbooks (Excel files). This is possible because
Excel stores your macro in a hidden workbook that opens automatically when Excel starts.
3. Click OK.
8. Now we can add this macro to the Quick Access Toolbar. Click the down arrow and click
More Commands.
13. You can now execute the macro. For example, select cell E2 and click on the smiley
button added to the Quick Access Toolbar.
Result:
14. When you close Excel, Excel asks you to save the changes you made to the Personal
Macro Workbook. Click Save to store this macro in a hidden workbook that opens
automatically when Excel starts. This way the macro will be available to all your workbooks
(Excel files).
Enable macros in Excel when the message bar appears. Change your macro security settings
in the Trust Center. To create macros, turn on the Developer tab.
1. When the message bar appears, click Enable Content to enable macros.
Note: by clicking Enable Content, the Excel file becomes a trusted document. As a result, you
won't see the Security Warning again when you open this specific Excel file in the future.
2. To change your macro security settings, on the Developer tab, click Macro Security.
4. The fourth option enables all macros. Use this security level if you are a beginner and
only typing your own macros at the moment. With this security level you don't have to enable
macros all the time.
5. If you're new to Excel VBA, let's create a simple macro.