Macro Lesson
Transcript of Macro Lesson
-
8/2/2019 Macro Lesson
1/24
#1CS 105 Fall 2006
Macros:
Sub Procedures You Record
What is a macro?
What is WithEnd With?
What is SubEnd Sub?
Relative vs. Absolute
-
8/2/2019 Macro Lesson
2/24
#2CS 105 Fall 2006
Macro Statements
A Macro always beginsa Sub statement
A Macro ends with anEnd Sub statement
-
8/2/2019 Macro Lesson
3/24
#3CS 105 Fall 2006
A Macro
To performmultipleactions onthe same
objectMacros use
With
End With
-
8/2/2019 Macro Lesson
4/24
#4CS 105 Fall 2006
Select and Dothe pattern
Note how first the Macro selects theobject, then executes the code
-
8/2/2019 Macro Lesson
5/24
#5CS 105 Fall 2006
More about Macros
Statements are color coded
Step Into command helps you debug amacro
Opening a file with a macro will prompt aquestion about viruses
-
8/2/2019 Macro Lesson
6/24
#6CS 105 Fall 2006
Record Macro
Comments begin with anapostrophe ()
Comments help the programmer,
others to read code
-
8/2/2019 Macro Lesson
7/24
#7CS 105 Fall 2006
Calling Macros
You can use macros easily because
they are on a Module sheet, that
makes them available to everything, so allyou do is write the macro name
-
8/2/2019 Macro Lesson
8/24
#8CS 105 Fall 2006
Finding the Stop Recording button!
We lost our little macro recorder button
If you lose yours, go toView/Toolbars/Stop Recording, and it will
appear on your spreadsheet or on atoolbar.
-
8/2/2019 Macro Lesson
9/24
#9CS 105 Fall 2006
Cell References in Macros
Absolute references
When we specify exactly what cells areaffected
Absolute cell address are constant
-
8/2/2019 Macro Lesson
10/24
#10CS 105 Fall 2006
An absolute Macro
Range selects the starting or activecell/cellsthe references below areabsolutewhenever the Macro runs, theseexact cells are selected
First, thecell is
selected,
then
a number
is placed
in it
-
8/2/2019 Macro Lesson
11/24
#11CS 105 Fall 2006
ActiveCell.FormulaR1C1
R1C1 is a reference style (you can set thisunder Tools/Options/General)
Dont worry,we wont be
using this
in class. We want
yo
u to
kno
wwhere it came
from.
-
8/2/2019 Macro Lesson
12/24
#12CS 105 Fall 2006
ActiveCell.FormulaR1C1 = "Income"
All that you need to know is that for theactive cell, the content is "Income"
"FormulaR1C1" refers to the contents ofthe active cell
You find this notation in Macros that youcreate
-
8/2/2019 Macro Lesson
13/24
#13CS 105 Fall 2006
FormulaR1C1 vs. Value
Range("D5").Select
ActiveCell.FormulaR1C1 = "8"
Range("D5").Select
ActiveCell.Value = "8"
These two code fragments producethe same results.
-
8/2/2019 Macro Lesson
14/24
#14CS 105 Fall 2006
Range("D5").Select
ActiveCell.FormulaR1C1 = "=SUM(A1: A10)"
Range("D5").Select
ActiveCell.Value = "=SUM(A1: A10)"
These two code fragments do not produce the same
results. FormulaR1C1 requires different cell
addressing, "=SUM(R[-4]C[-3]:R[5]C[-3])"
Not used for a formula, though
-
8/2/2019 Macro Lesson
15/24
#15CS 105 Fall 2006
What if you want to vary the cells?
-
8/2/2019 Macro Lesson
16/24
#16CS 105 Fall 2006
Relative references
We dont always want the event to happen inthe same place
Cell addresses change
Visual Basic uses Offset to indicate spaceaway from active cell
-
8/2/2019 Macro Lesson
17/24
#17CS 105 Fall 2006
Recording a Macrowith Relative References
Before you start recording your Macro, clickon the
RelativeReference
button
-
8/2/2019 Macro Lesson
18/24
#18CS 105 Fall 2006
Relative Cell References
ActiveCell.Offset(2, 0).Range("A1").Select
Means the cell two rows below the active cell, inthe same column
-
8/2/2019 Macro Lesson
19/24
#19CS 105 Fall 2006
ActiveCell.Offset(2, 0).Range("A1").Select
What does
ActiveCell.Offset(2, 0).Range("A1").Select mean?
-
8/2/2019 Macro Lesson
20/24
#20CS 105 Fall 2006
Range("A1").Select
This part of the code tells you how manycells have been selected.
ActiveCell.Offset(2, 0).Range("A1").Select means1 cell is selected
ActiveCell.Offset(2, 0).Range("A1:C1").Selectmeans 3 cells in a row have been selected
-
8/2/2019 Macro Lesson
21/24
#21CS 105 Fall 2006
Activate or Select?
Range("B3").Select
Selection.Interior.ColorIndex = 6
Range("B3").Activate
Selection.Interior.ColorIndex = 6
Both code fragments have the same result
-
8/2/2019 Macro Lesson
22/24
#22CS 105 Fall 2006
This Relative Macro Causes a Crash
-
8/2/2019 Macro Lesson
23/24
#23CS 105 Fall 2006
Run it line-by-line to find bug
Code runs OK until it reaches
the line under the highlighted
line. You can turn your
attention to the problem lineof code!
-
8/2/2019 Macro Lesson
24/24
#24CS 105 Fall 2006
To Summarize
What is a macro?
What is WithEnd With?
What is SubEnd Sub?
Relative vs. Absolute