Вы находитесь на странице: 1из 4

A Beginner's Guide on how to Record,

Modify and Run Your First Excel Macro


This is the 19th weekly Excel tip at www.MrExcel.com. Many of the Excel tips involve some
sort of macro trick. This week, for the Excel users who have never written a macro, I offer a
primer on how to record and then customize a useful Excel macro.
et!s say you have "## rows of address data like that shown in the top fi$ure at the left. The
name field is in column %, the street address in column &, and the city in column '.
(our $oal is to convert the data to a sin$le column like that shown in the second fi$ure.
This simple pro)lem will )e used to illustrate how to record, modify, and then run a simple
macro.
*or Excel 9+ users, %fter recordin$ the macro, Excel will put your macro on a sheet called
Module1 in your work)ook. (ou can -ust click on the sheet to access the macro.
%lthou$h there are "## records in this worksheet, I want to record a tiny )it of the macro that
takes care of -ust the first address. The macro will assume that the cellpointer is on the first
name. It will insert three )lank rows. It will copy the cell to the ri$ht of the ori$inal cell to the
cell underneath the ori$inal cell. It will copy the city cell to the cell . rows under the ori$inal
cell. It should then move the cell pointer down so it is on the next name.
The key is to think this process out )efore you record it. (ou don!t want to make a lot of
mistakes when recordin$ the macro.
/o, put your cell pointer in cell %1. 0o to the menu and select Tools 1 Macro 1 2ecord new
Macro. The 2ecord Macro dialo$ su$$ests a name of Macro1. This is fine, so hit 34.
The Excel macro recorder has one very stupid default settin$ which you a)solutely must
chan$e in order to have this macro work. In Excel 9+, $o to Tools 1 Macro 1 5se 2elative
2eferences In Excel 967.##8, click the second icon on the /top 2ecordin$ Tool)ar. The icon
looks like a tiny worksheet. % red cell in '8 points to another red cell in %8. The icon is
called 2elative 2eference. 9hen this icon is :on;, there is some color surroundin$ the icon.
The icon remem)ers the last settin$ from the current Excel session, so you mi$ht have to
click it a couple of times to fi$ure out which method is on or not. In Excel .##6, use <iew =
Macros = 5se 2elative 2eferences.
34, we are ready to $o. *ollow these steps,
>it the down arrow once to move to cell &1.
>old down the shift key and hit the down arrow twice to select rows ., 8, and "
*rom the menu, select Insert, then select 2ows to insert three )lank rows.
>it the up arrow and then the ri$ht arrow to move to cell &..
>it 'trl ? to cut cell &..
>it the down arrow, the left arrow, then 'trl < to paste into cell %..
>it up arrow, ri$ht arrow, ri$ht arrow, 'trl ?, left arrow, left arrow, down arrow, down
arrow, 'trl < to move '1 to %8.
>it the down arrow twice so that the cell pointer is now on the next name in 2ow %+.
'lick the @/top 2ecordin$@ icon on the tool)ar to stop recordin$ the macro.
9ell, you have recorded your first macro. et!s take a look. 0o to Tools 1 Macro 1 Macros.
*rom the list, hi$hli$ht Macro1 and press the Edit )utton. (ou should see somethin$ that
looks like this.
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 4/18/99 by MrExcel Reader
'
'
ActiveCell.O!et(1" #).Ra$%e(&A1'A(&).Select
Selectio$.E$tireRo).*$!ert
ActiveCell.O!et(+1" 1).Ra$%e(&A1&).Select
Selectio$.Cut
ActiveCell.O!et(1" +1).Ra$%e(&A1&).Select
ActiveS,eet.-a!te
ActiveCell.O!et(+1" .).Ra$%e(&A1&).Select
Selectio$.Cut
ActiveCell.O!et(." +.).Ra$%e(&A1&).Select
ActiveS,eet.-a!te
ActiveCell.O!et(." #).Ra$%e(&A1&).Select
E$d Sub
>ey, if you are not a pro$rammer, that pro)a)ly looks pretty intimidatin$. Aon!t let it )e. If
there is somethin$ you don!t understand, there is excellent help. 'lick your cursor somewhere
in the keyword 3ffset and hit *1. Brovided you installed the <&% help file, you will see the
help topic for the 3ffset keyword. The help tells you the syntax of the statement. It says that
it is 3ffsetC2ow3ffset, 'olumn3ffsetD. /till not very clearE ook for the $reen underlined
word @example@ near the top of the help. Excel!s <&% examples will allow you to learn what
is $oin$ on. In the example of 3ffset, it says to activate the cell two rows )elow and three
rows to the ri$ht of the current cell, you would use,
ActiveCell.O!et((" .).Activate
34, so that is a clue. The offset function is a way of movin$ around the Excel spreadsheet.
0iven this )it of information, you can kind of see what the macro is doin$. The first offsetC1,
#D is where we moved the cellpointer down to %.. The next 3ffset is where we moved up one
row C71 rowsD and over 1 column. (ou may not understand anythin$ else in the macro, )ut it
is still useful.
0o )ack to the Excel worksheet. But your cell pointer in cell %+. 'hoose Tools 1 Macro 1
Macros 1 Macro1 1 2un. The macro runs and your second address is formatted.
(ou may )e sayin$ selectin$ this whole lon$ )i$ strin$ of commands is harder than -ust
formattin$ )y hand. 34, then do Tools 1 Macro 1 Macros 1 3ptions. In the shortcut )ox, say
'trl F w is the shortcut key for this macro. 'lick 34, then dismiss the Macro dialo$ with
'ancel. Gow, when you hit 'trl w, the macro will run. (ou can format an address in a sin$le
keystroke.
%re you ready for the )i$ timeE >ow many addresses do you have leftE I hit 'trl w a few
times, so I have 89+ left. 0o )ack to your macro. 9e are $oin$ to put the entire macro code in
a loop. Insert a new line that says @Ao 5ntil activecell.value H @@ @ )efore the first line of
macro code. Insert a line that says @oop@ )efore the End /u) line. The Ao loop will execute
everythin$ )etween the Ao and the oop line until it runs into a )lank line. The macro now
looks like this,
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 4/18/99 by MrExcel Reader
'
'
/o 0$til ActiveCell.1alue 2 &&
ActiveCell.O!et(1" #).Ra$%e(&A1'A(&).Select
Selectio$.E$tireRo).*$!ert
ActiveCell.O!et(+1" 1).Ra$%e(&A1&).Select
Selectio$.Cut
ActiveCell.O!et(1" +1).Ra$%e(&A1&).Select
ActiveS,eet.-a!te
ActiveCell.O!et(+1" .).Ra$%e(&A1&).Select
Selectio$.Cut
ActiveCell.O!et(." +.).Ra$%e(&A1&).Select
ActiveS,eet.-a!te
ActiveCell.O!et(." #).Ra$%e(&A1&).Select
3oo4
E$d Sub
0o )ack to your Excel sheet. But the cell pointer on the next name. >it 'trl w and the macro
will format all of your records in a few seconds.
The authors of Excel )ooks say that you can not do anythin$ useful )y recordin$ a macro.
Got trueI *or the person who was $oin$ to have to cut and paste J## times, this macro is very
useful. It took a few minutes to record and customize. (es, professional pro$rammers will
point out that the code is horri)ly inefficient. Excel puts a whole )unch of stuff in there that it
does not need to put in there. (es, if you knew what you are doin$, you can accomplish the
same task with half the lines which will run in 1.. seconds instead of 8 seconds. /3 9>%TE
8 seconds is far faster than the 8# minutes the task would have taken.
/ome more tips for )e$innin$ macro recorders,
The apostrophe is used do indicate a comment. %nythin$ after the apostrophe is
i$nored )y <&%
This is o)-ect oriented pro$rammin$. The )asic syntax is o)-ect.action. If a o)-ect
oriented compiler were playin$ soccer, it would say @)all.kick@ in order to kick the
)all. /o @/election.'ut@ says to do an @edit 1 cut@ on the current selection.
In the a)ove example, the 2an$e modifiers are relative to the active cell. If the active
cell is in &. and you say @%ctive'ell.2an$eC@%1,'8@D./elect@, then you select the 8
row )y 8 column area startin$ in cell &.. In other words, you select &.,A". /ayin$
@%ctive'ell.2an$eC@%1@D@ says to select the 1 x 1 cell ran$e startin$ with the active
cell. This is incredi)ly redundant. It is eKuivalent to sayin$ @%ctive'ell./elect@.
/ave your work)ook )efore runnin$ a macro for the first time. This way, if it has an
error and does somethin$ unexpected, you can close without savin$ and revert )ack to
the saved version.
>opefully this simple example will $ive you novice macro recorders the coura$e to record a
simple macro the next time you have a recurrin$ task to perform in Excel.
*or more tips like this pa$e, check out MrExcel!s )ook,

Вам также может понравиться