Microsoft Excel - Macros

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Microsoft Excel - Macros

1 Keystroke Macros

A macro is a recorded operation in Excel (e.g. a recorded series of Excel functions and
commands). It means that you can avoid specifying the operation repeatedly. Instead, you
record the macro once, then recall it using a shortcut key.

Using macros can save you a large amount of time. Here you will see how.

It is possible to create macros using the Record Macro feature on the Tools-Macro menu.
Open a new Excel workbook. Enter the number 23 in cell B2 and keep that cell selected
as the active cell. Let’s say you wish to create a macro to format a number in a cell so
that two decimal places are displayed.

Select the menu command, Tools → Macro → Record New Macro... , and a Record
Macro dialog box appears.

Enter TwoDec as the macro name. Enter the letter S (capital letter) in the field after the
Ctrl+. Now, select the OK button. Note the Recording flag in the status panel at the lower
left of the screen. And, somewhere on your screen, you should see a small Stop
Recording box.

Carefully go through the steps required to reformat the number in the cell. An easy way to
do this is to right-click on the cell and select Format Cells. Then click on the Number tab,
select Scientific from the list on the left and make certain that 2 is in the Decimal places:
field. Click OK. Click on the Stop button (the button on the left side of the small Stop
Recording button box).

Test whether your macro has been created. Put a number in another cell, and, with that
cell active (highlighted), press Ctrl-Shift-s. Did it work? We hope so.
Now, let’s take a look at the macro. Macros are recorded in the Visual Basic programming
language. Switch over to the Visual Basic Editor by pressing Alt-F11. The editor screen
should appear and show three partitions. In the upper left, there should be a Project –
VBA Project window. There, you should see a Modules entry. Click on the + sign to the
left of this and a branch to Module1 should open up. Double-click on the Module1 entry
and a window should open up on the right with the VBA code in it for your TwoDec macro.
After a few comments that give the title, date recorded, and shortcut key, the macro
program should appear in concise form as

Sub TwoDec()
'
' SciTwoDec Macro
' Macro recorded 13/03/97 by . . .
'
' Keyboard Shortcut: Ctrl+Shift+S
'
Selection.NumberFormat = "0.00E+00"
End Sub

The Visual Basic code is color-coded: comments in green, keywords in dark blue, and
everything else in black. Edit the Visual Basic code to,

Sub TwoDec()
'
' SciTwoDec Macro
' Macro recorded 13/03/97 by . . .
'
' Keyboard Shortcut: Ctrl+Shift+S
'
Selection.NumberFormat = "0.0000E+00"

End Sub

Now go back to the Excel sheet by pressing Alt-F11 again. Make B2 active again and run
your macro by pressing Ctrl-Shift-s.

You can make Excel do almost anything by recording and editing Marcos. Lets do a
slightly more ambitious example. First, save your Excel workbook. Then select the menu
command Tools → Macro → Record New Macro... , and enter RainGauge as the macro
name. Enter the letter T (capital letter) in the field after the Ctrl+ and select the OK button.

Now select the menu command Insert → Worksheet. A new worksheet will be created.
Type the following entries into this worksheet, then save the workbook, then stop the
recording.
By running your Macro RainGauge 4 times, create 4 identical worksheets, then enter a
rain gauge number (1 to 4) in cell C2 of each.

2 Exercise - Using Macros

Record a macro which, for one of your catchments:

• Enters some imaginary data in cell range B5 to F16. You can do this by using Excel’s
random number function. For example, type “=100 x RAND()” in cell B5 and use the
Fill command to fill in all 60 cells.

• Colours the cells with the month names and the cells with the year numbers in the
same colour, and the cells with the data in a second colour.

• Calculates the total rainfall for each year, and the average rainfall for each month
over the five years, and displays these on the spreadsheet.

Run this macro for all four of your rain gauges, so that they look something like this,

TIP: If you want to interrupt your macro while it is running, press the Esc button then
select END
Plot the monthly average rainfall for all four catchments on one Excel Chart, so that they look
something like this,

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy