0% found this document useful (0 votes)
20 views7 pages

Macros A

The document provides a comprehensive guide on creating and using macros in Excel, including instructions for recording macros with absolute and relative references. It covers the steps to record macros for totaling values and coloring cells, as well as how to edit macros using Visual Basic. The document emphasizes the importance of understanding both absolute and relative references to enhance macro functionality.

Uploaded by

Yared Addisu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Macros A

The document provides a comprehensive guide on creating and using macros in Excel, including instructions for recording macros with absolute and relative references. It covers the steps to record macros for totaling values and coloring cells, as well as how to edit macros using Visual Basic. The document emphasizes the importance of understanding both absolute and relative references to enhance macro functionality.

Uploaded by

Yared Addisu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Macros

Introduction
A macro is a series of instructions that Excel follows from a single command.
The macro can be run, or started, in various different ways. You can set it to run from the
keyboard by giving it a control-plus key combination; by inserting a button on the Quick
Access toolbar (similar to how we input the Form button) and setting the macro code
behind it; or through the menu system.
To be able to easily write all kinds of macros, you would need to know a programming
language (Microsoft Visual Basic); while we will cover Visual Basic, we will not cover the
entire programming language, and in this lecture we will cover some basic macro writing
(and recording).

Absolute and Relative References


Within macros, you have the option of using absolute references to perform instructions on
specific cells, such as moving to a specific cell, calculating the sum total of the specific cells
above that cell and formatting the result output as an Accounting format. You also have the
option of using relative references to perform instructions, such as moving to a cell a number of
spaces away from your current position.

1. Open the SalesData.xlsx file, found in Canvas.


2. Make sure you have the Developer tab on your Ribbon.

Using Absolute References


First let’s record a macro that will total the values for columns E through I and then alter the
format of the totals.

3. Move to the Developer tab and click on the [Record Macro] button. The Record
Macro dialog box opens. This allows you a place to name the macro, set a shortcut key,
tell Excel where to store the macro and write a description of the macro being recorded.

4. Set the Macro name: as TotalExample and the Shortcut key: to Ctrl+ q.
Note*** Case matters!! - avoid existing key combinations: [Ctrl s] is Save, [Ctrl c] is Copy,
[Ctrl v] is Paste, etc.

5. Store macro in: This Workbook.


Note*** The alternative to storing in ‘This Workbook’ is to store it in your Personal Macro
Workbook. If you are recording a macro that you want to be able to use in multiple workbooks
across Excel, you’d save it there – if you want the macro to be able to be distributed with the
workbook it’s being used in than you’d save it in the workbook.
6. Enter into the Description: “Macro to total columns E through I and reformat the
output.”
7. Click [OK] to start recording.

Excel now begins recording each keystroke you make. A recording button appears on the
bottom frame (shown circled in red below).

8. Click in cell E1 to make sure the macro starts in cell E1


9. Press [Ctrl-Down Arrow] to move the active cell to the end of column E
10. Press [Down Arrow] to move to cell E47.
11. From the Home tab, click the [AutoSum] button (designated by the ∑ symbol).
12. Confirm the formula says =SUM(E2:E46) and press [Ctrl-Enter].
13. Press [Ctrl-b] (or click on the [Bold] button).
14. Drag the formula in E47 across to I47 to get the totals for all the columns.
15. Highlight cells F47 through I47 and then click the $ in the Number area of the Home
tab to set those cells to Currency format.
16. Click on the recording button on the bottom panel, or click on the Developer tab then
on [Stop Recording] to end the macro.

Now test the macro:

17. Delete the information in cells E47 through I47.


18. Press [Ctrl q] to run the macro.

To demonstrate what is meant by Absolute References:

19. Select rows 46 and 47 then press [Ctrl c] to copy them.


20. Right click on cell A50 and under Paste Options… choose Values.
21. Select rows 46 and 47 again and delete the contents (just the contents, not the rows)
22. Run the macro again ([Ctrl q]) – your totals appear in row 47, with the new totals
differing from those in row 51.

This is a clear example of absolute references - the macro only works on the cells for which it
was recorded. Even though a control key combination was used (Ctrl-arrow down) to move to
the foot of the column, the next move (to cell E47) was recorded as a move to a fixed cell.

Let’s try another:

23. Press [Ctrl Home] to move to cell A1


24. On the Developer tab click on the [Record Macro] button
25. Set the Macro name: as Color and the Shortcut key: to Ctrl+ m
26. Store Macro in: This Workbook.
27. Put a Description of “color a Region”
28. Click [OK] to start recording
29. Press [Ctrl f]
30. In the Find what: box type Central
31. Click on [Find Next]
32. From the Home tab, choose the pull down on the Fill button and go to More Colors.
33. Choose a blue-green color.
34. Click on the recording button on the bottom panel, or click on the Developer tab then
on [Stop Recording] to end the macro.
You should now have a macro that colors cells that contain Central (in the Region column) a
blue-green color.

Test out the macro:


35. Click in cell C8.
36. Press [Ctrl m] three times – you’ll see three of the cells color in, C9, C10 and C13.
Cells that say ‘East or West’ were not colored in, and neither are cells C4 or C5 – that’s
because you were already below them when you ran the macro.
Later we’ll learn how to edit the macro so it colors ALL the Central cells, not just one at a time.

Using Relative References


You saw earlier how the TotalExample macro failed to work properly because it was using
absolute references. Let’s correct it:
37. Delete the data in row 46.
38. First, click in cell A1 to make it the active cell then click on [Record Macro] on the
Developer tab
39. Set the Macro name: as NewTotalExample and the Shortcut key: to Ctrl+ w
40. Store Macro in: This Workbook
41. Set the Description to: ‘Fix earlier TotalExample macro’.
42. Click [OK] (or press Enter) to start recording.
43. From the Developer tab, click on the [Use Relative References] button in the Code
area.
44. Press [Ctrl - up arrow] to make sure the active cell is at the top of the column
45. Next, press [Ctrl - down arrow] to move the active cell to the end of the column
46. Press [Down Arrow] to move to cell A46.
47. From the Home tab, click the [AutoSum] button (designated by the ∑ symbol).
48. Confirm the formula says =SUM(A2:A45) and press [Ctrl-Enter].
49. Click on the recording button on the bottom panel, or click on the Developer tab then
on [Stop Recording] to end the macro.
50. Click in cell E1 and press [Ctrl w] to run the macro – it should work for column E.
51. Run the macro for columns E, F, G, H and I, starting in any cell in each column above
row 46.

If you look at the formulas in row 46, you’ll see that the references aren’t completely relative,
as it is totaling at most 45 of whatever column you choose, not whatever references are
available.
To demonstrate this further:
52. Click in cell E5.
53. Press [Ctrl w] to run the macro.
54. Click in cell E7.
55. Press [Ctrl w] to run the macro again.
Each time, it goes to the first empty cell (E48 then E49) and calculates – not 45, but from
where you were to where you are. If you had 46 values, it would still only calculate 45 of them:
56. Click in cell E2.
57. Press [Ctrl w] to run the macro again. Check the formula in cell E52. Weird, huh?

You’ll see how to correct this later (but it needs a little programming).

58. Click on the [Use Relative References] button to turn it off

Now let’s look at using both absolute and relative references.


59. Click on the plus sign at the bottom to add a worksheet.
60. Click on the Developer tab and click on [Record Macro]
61. Set the Macro name: as Every5 and the Shortcut key: to Ctrl+ e
62. Store Macro in: This Workbook
63. Amend the Description: to “Copy every 5”
64. Click [OK] to start recording

Be very careful with recording the instructions correctly – if you make a mistake, it is probably
best to delete the macro and start over. If you have to do this, stop recording, and click on
Macros from the Developer tab. In the dialog box, highlight the macro and click Delete.

65. First, check that relative references are off (the button should not be highlighted in
orange-brown)
66. Click on the Sales tab then press [Ctrl Home] to make A1 the active cell
67. Now click on [Use Relative References] to turn relative references on
68. Hold down [Ctrl – Shift - right arrow] to select row 1
69. Press [Ctrl c] to Copy.
70. Press [Ctrl - Page Down] (or click on the Sheet1 tab) to move to the empty sheet
71. Click in cell A1 and press [Ctrl v] to Paste
72. Click in cell A2
73. Click on the Sales tab to move back to the original data
74. Click on cell A6
75. Hold down [Ctrl – Shift - right arrow] to select the row.
76. Press [Ctrl c] to Copy.
77. Press [Ctrl - Page Down] (or click on the Sheet1 tab) to move to the empty sheet
78. Click on cell A2 and press [Ctrl v] to Paste
79. Click on the recording button on the bottom panel, or click on the Developer tab then
on [Stop Recording] to end the macro.
80. Turn off [Use Relative References]

Next, to complete the macro, we’ll edit it.


Editing a Macro
Thus far, recording macros has been relatively easy. Changing them is much more difficult.
When you edit a macro you are taken into the Visual Basic Editor, and you need to understand
exactly what the programming code is doing. Even if you don’t know the entire Visual Basic
programming language, you can make changes and turn simple recorded macros into more
useful ones.
81. On the Developer tab, click on [Macros]
82. Select Every5 then click on [Edit]

You have now opened the Visual Basic Editor, and code at least similar to the following should
show:

Sub Every5()
'
' Every5 Macro
' Copy every 5
'
' Keyboard Shortcut: Ctrl+e
'
Sheets("Sales").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
ActiveSheet.Next.Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Range("A1").Select
Sheets("Sales").Select
ActiveCell.Offset(5, 0).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Next.Select
ActiveCell.Select
ActiveSheet.Paste
End Sub

Don't panic if you don't understand what appears on the screen, if you compare it carefully with
the original instructions then you might get some idea of what is happening.
Note the line which says:
ActiveCell.Offset(1,0).Range("A1").Select
This is a relative reference – it says to move the active cell down 1 row and across 0 columns
from the current active cell.
Had we been using absolute references this line would have just read Range("A2").Select – for
move to and select cell A2.
Repeating an Action
Now let’s make some changes to the macro. The original objective of the macro was to select
EVERY fifth row - the instructions which have been recorded contains the first row, but it now
needs to be repeated 8 times (as there are 44 rows in the data set [not including the header]).
To do this, you need to add extra lines to the middle and end of the program:
83. Move the typing position to the start of the code to be repeated -
ActiveCell.Offset(1,0)…
84. Add the following line of code for k=1 to 8 then press [Enter]
85. Click at the end of the next to last line right above End Sub – (on my example code
above, this is where it says ActiveSheet.Paste)
86. Press [Enter] and type the following line of code: next k

These extra two lines of code create a loop to be repeated 8 times. The loop counter, k, starts
at 1 and increases by 1 each time the code is run. When it gets to 8, the loop ends. In other
words, you’re telling Visual Basic first, I’m going to loop through these instructions 8 times (For
k = 1 To 8). When the code gets to Next k, you are telling Visual Basic – go back to where I
said “For k = 1 To 8” and start over.

87. Close the Editor window by clicking on the [Close] window button - the changes to the
code are saved automatically

To see this editing demonstrated on a different macro, watch the lecture video.

88. Delete the values stored on Sheet1 then run the macro – if you get an error using the
shortcut (which can happen depending on what you changed in Visual Basic, run the
macro by clicking on Macros, highlighting the Every5 macro and clicking Run.

You should find that 8 rows (every 5th one) are copied across to Sheet1.

89. See if you can perform a similar edit on the Color macro to color all the Central
regions. You will need to increase the final value of the loop to at least 23 (as there are
23 orders in the Central region).

Click on the File tab, save the file to your computer as a Macro Enabled (.xlsm) file, then
answer the macros A assessment based on your file and/or comprehension of the lecture.

You might also like