3.0 Record Macro Functions, Debugging and testing, V-Lookup function Flashcards
Recording Macros
to develop a macro is to write it directly in VBA
- when you record a macro, VBA code automatically generated
- two main purposes
- to automate a sequence of Excel command used repeatedly;
- to learn how to write VBA code
- by recording macro we can obtain necessary instructions or syntax to do specific task in VBA
- eg. want to learn how to move (select) cell below relative to active cell
▪ select “Relative References” in toolbar (Developer > Code > Use Relative Reference)
▪ click “record macro”
▪ click down arrow once in worksheet
▪ click “stop recording”
▪ look at macros created and find related line
- example on right: this statement sets number format of a cell to scientific notation with two decimal places
debugging and testing
process of hunting down and correcting errors (bugs)
- three major varieties of errors
- syntax errors
- executive (run-time) errors
- logic errors
three major varieties of errors
debugging and testing
syntax errors
executive (or run-time) errors
logic errors
passive debugging
debug button interrupts program execution and puts you into break mode
active debugging
by pressing F8 key (or Fn F8) you are stepping through the program
- to stop execution of program at particular location set up a breakpoint by pressing F9 (or Fn F9) or click
on the margin
- to remove breakpoint, click the dot
syntax errors
violate spelling conventions, placement of parentheses, number formatting, etc
▪ eg. Compile error: Invalid character
executive (or run-time) errors
result when a statement attempts an invalid operation
▪ eg. Run-time error ‘11’: Division by zero
logic errors
due to faulty program logic (garbage in, garbage out)
Vlookup
vertical (top to bottom)
- looks up value in LEFT column of a table, jumps to column N of the table (the matched column is column 1) and returns value in that location
hlookup
horizontal (left to right)
- looks up value in top row of a table, jumps down to row N of same table (same column) and returns value
in that location
Message box display
Msg = “Do you want to continue?”
Style = vbYesNo + vbQuestion + vbDefaultButton2
Title = “MsgBox DEMO”
Response = MsgBox(Msg, Style, Title)
Input Box Display
Msg = “Please enter the length of the beam”
Length = InputBox (Msg, “Length (m)”)