notes7
notes7
Introduction
Before becoming familiar with VBA (Visual Basic for Applications) we will
focus on a related “capability” of Microsoft Office Products to record and
replay user-entered keystrokes and other input device information (mouse
movements, clicks, etc).
One of the main advantages of macros is that they can “flawlessly replay”
complex sequences of keystrokes (thus saving us a lot of time and effort).
One of the major disadvantages of macros is that they can “mindlessly and
swiftly replay” a complex sequence of keystrokes that should never have
been applied to the task at hand. Thus, macros are “double edge swords”
with both advantages and disadvantages.
The easiest way to create a macro in Excel is just to let Excel “listen in” on
what keys/etc you are pressing and store these to be later executed by a
single keystroke. These “saved keystrokes” can be viewed and edited later.
Another way to create a macro is to start from scratch using the VBA Editor.
The largest limitation of macros is that they cannot accept information “on
the fly” (that is, there is no “argument list” to pass information to the
macro). Hence, you can write a macro to “add three” to the cell
immediately to the left of the current cell, but you cannot write a macro to
add “some number” to that same cell.
CHEN 3600 – Computer-Aided Chemical Engineering
Chemical Engineering Department Notes 7
EWE: “Engineering With Excel” Larsen Page 2
Security:
Recording Macros
Note: The appearance of this toolbar varies with the version of Excel
being used.
Unselected Selected
Normal/Bold
Absolute/Relative
What has happened is that the “stop recording toolbar” has gotten
“unchecked” in the “View/Toolbars” menu. If this happens, you should
stop recording the current macro (delete it later) by selecting
“Tools/Macro/Stop Recording” and then selecting”
Note that the “stop recording” toolbar does not appear in the “toolbar list”
unless you are actually recording a macro. It ALWAYS appears in the
“customize list”.
Sub Macro4()
'
' Macro4 Macro
' Macro recorded 2/22/2005 by Tim Placek
'
' Keyboard Shortcut: Ctrl+n
'
Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
Range("B2").Select
ActiveCell.FormulaR1C1 = "2"
Range("C3").Select
ActiveCell.FormulaR1C1 = "3"
Range("D4").Select
ActiveCell.FormulaR1C1 = "4"
Range("E5").Select
ActiveCell.FormulaR1C1 = "5"
Range("F6").Select
End Sub
Try the macro in several other places (move your cursor to different
locations before pressing “Cntl-n”. Notice that wherever you locate the
cursor, the data will be created in the locations we originally put the data
(A1-E5).
Notice that we can change any of these items and our macro will be
appropriately edited. Hence, you can make mistakes and “fix them” or make
the macro do new things.
In this case, change the values to 0, 0, 0, 0, 0. Test your macro. You could
also change the addresses to A5, B4, C3, D2, E1. You could also type
additional lines to add more “typing” or delete some lines.
Relative Mode: This time, do the same thing EXCEPT when you start the
recording, make sure “relative” mode is selected.
CHEN 3600 – Computer-Aided Chemical Engineering
Chemical Engineering Department Notes 7
EWE: “Engineering With Excel” Larsen Page 6
Sub Macro5()
'
' Macro5 Macro
' Macro recorded 2/22/2005 by Tim Placek
'
' Keyboard Shortcut: Ctrl+p
'
ActiveCell.Offset(-2, -2).Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
ActiveCell.Offset(1, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "2"
ActiveCell.Offset(1, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "3"
ActiveCell.Offset(1, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "4"
ActiveCell.Offset(1, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "5"
ActiveCell.Offset(1, 1).Range("A1").Select
End Sub
Notice that the latter case produces an error since the macro instructs the
computer to go to a position that doesn’t exist.
CHEN 3600 – Computer-Aided Chemical Engineering
Chemical Engineering Department Notes 7
EWE: “Engineering With Excel” Larsen Page 7
Timothy D. Placek
CHEN 3600 - Computer Aided Chemical Engineering
Fall 2005
4. Highlight cells A1:H3 and (1) change the background color to “tan” (2)
make the characters “bold” (3) change the font size to “12” and
change the font to “Verdana”.
Timothy D. Placek
CHEN 3600 - Computer-Aided Chemical Engineering
Fall 2004
CHEN 3600 – Computer-Aided Chemical Engineering
Chemical Engineering Department Notes 7
EWE: “Engineering With Excel” Larsen Page 8
Try the macro in several other places. Notice that wherever you locate the
cursor, the data will be placed in that location (this is a relative reference).
If this doesn’t work, you did not get the relative/absolute mode setting
correct.
CHEN 3600 – Computer-Aided Chemical Engineering
Chemical Engineering Department Notes 7
EWE: “Engineering With Excel” Larsen Page 9
Sub ProgrammerName()
'
' ProgrammerName Macro
' Macro recorded 9/21/2004 by Tim Placek
'
' Keyboard Shortcut: Ctrl+n
'
ActiveCell.Select
ActiveCell.FormulaR1C1 = "Timothy D. Placek"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "CHEN 3600 - Computer-Aided Chemical Engineering"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "Fall 2004"
ActiveCell.Offset(-2, 0).Range("A1:H3").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Arial"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With Selection.Font
.Name = "Verdana"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With Selection.Interior
.ColorIndex = 40
.Pattern = xlSolid
End With
End Sub
Notice that we can change any of these items and our macro will be
appropriately edited. Hence, you can make mistakes and “fix them” or make
the macro do new things.
CHEN 3600 – Computer-Aided Chemical Engineering
Chemical Engineering Department Notes 7
EWE: “Engineering With Excel” Larsen Page 10
In this macro we will enter a number in a cell based on the contents of the
cell immediately to the left of the cell, namely, we will multiply the value by
10.
5. Use the cursor keys to move the cursor to the cell just to the right of
E4 (namely F4).
Sub MultiplyByN()
'
' MultiplyByN Macro
' Macro recorded 9/21/2004 by Tim Placek
'
' Keyboard Shortcut: Ctrl+m
'
ActiveCell.Select
ActiveCell.FormulaR1C1 = "=RC[-1]/10"
ActiveCell.Offset(0, 1).Range("A1").Select
End Sub
Let’s alter the macro to take the value stored in A1 and use it in place of
“10”. Notice that the value in A1 is refered to by the general notation R1C1.
We could have gotten this same result if when we were recording the macro
we pressed the F4 key after pointing to the cell A1.
Notice that the numbers are getting bigger now because we have A1=0.1
(dividing by 0.1)!
Sub MultiplyByN()
'
' Multiply Macro
' Macro recorded 9/21/2004 by Tim Placek
'
' Keyboard Shortcut: Ctrl+m
'
ActiveCell.FormulaR1C1 = "=RC[-1]/R1C1"
ActiveCell.Offset(0, 1).Range("A1").Select
End Sub
Let’s begin by writing a macro that makes the background color of the cells
in the range A1:D6 “yellow”.
… or we can select the “Options…” menu and assign the macro to a key
(Cntl-u for example).
Make sure you have some cells selected before you run the macro again.
You should make the cells “Green” by this macro.
This will put the characters “CHEN3600” in green cells in a very small red
font with bold turned on.
CHEN 3600 – Computer-Aided Chemical Engineering
Chemical Engineering Department Notes 7
EWE: “Engineering With Excel” Larsen Page 15
6. In order to read a value out of a cell and put it in others, use the
following:
We will explore many of these issues again when we formally consider VBA
programming in the next chapter.
Questions
5. What code should be written to save the value of the contents of cell
C8 to the variable x? Use “absolute methods”.
6. What code should be written to save the value of the contents of cell
C8 to the variable y if the currently selected cell is D8? Use “relative
methods”.
7. What code should be written to put the sum of “x” and “y” into cell F9?
“Use absolute methods”
Answers
Answers
2. ActiveCell.Offset(1, 0).Range("A1").Select
or
ActiveCell.Offset(1, 0).Select
4. ActiveCell.Value = "hello"
5. x = Cells(8,3).Value
6. y = ActiveCell.Offset(0,-1).Value
7. Cells(9,6).Value = x+y