VBA Flashcards
learn VBA
Select all cells in Current region, VBA and keyboard?
Selection.CurrentRegion.Select; Ctrl+Shift+8 (*)
Return the color index to a Message Box
MsgBox Selection.Interior.ColorIndex
How do you force the declaration of variables?
Tools/Options (in VBA). Add tick to “Require Variable Declaration”.
What does Option Explicit do?
Make you declare all variables
What is the scope of a variable
How far it reaches
How is a variable declared locally (within a single procedure)?
By using a Dim or Static statement within the procedure.
How is a variable declared within the current module?
By using a Dim statement before the first Sub or function.
What does a Public statement do?
Declares a variable in all Subs and Functions in all modules in the current workbook
What does Static do when used in place of Dim?
Freezes the variable
What is the shortcut for Step Over in debugging?
Shift + F8
Shortcut for Debug run to cursor (runs up until the cursor)
Ctrl + F8
How do you add a Watch
Debug menu, Add Watch
How do you step out of a sub procedure
Ctrl + Shift + F8
When debugging, how do you add a break point and what does it do?
LHB in left margin. It breaks the sub.
How do you halt your code if debugging when a criteria is satisfied?
Insert “Debug.Assert” with a criteria such as “s < 5”. Or you can use “Debug Menu, Add Watch.. (Break When Value is True)
How do you change from absolute to relative references when recording a Macro?
Developer Tab, Use Relative References
What is counter intuitive about Debug.Assert
Debug.Assert runs until the criteria is FALSE
What should you avoid when using Dim in functions
Redimming of arguments
in functions, why (a as Double, b as Double) As Double
Because we also DIM th output
How do you debug a function?
Break point. In Excel, click into the function, hit Enter. in VBA you can now step through.
Why should you avoid message boxes in cells?
Because answer does not get returned to cell and because if copy to multiple cells, you get as many pop ups!
The order of VBA operators
^, -, *,/ ,\, mod, +/-, string, relational operators, logical operators
What are properties
Attributes of objects
What are methods
actions to be taken on objects
What are events
happenings that objects respond to
How do you refer to the range “A1” in the worksheet “Data”
Worksheet (“Data”).Range(“A1”)
What is a difference between a range and a selection
A range does not change
How to you assign a value to a variable “x” via an Input Box
x = InputBox (“????”)
How do you assign the value of the 2nd cell across and 2nd cell down to x
x = Selection.Cells (2,2)
How do you move 2 cells down and 2 cells to the right
ActiveCell.Offset (2,2)
How many variables is it good practice to have?
As many as possible as you can monitor in the Locals window
How do you assign a cell to a variable?
Dim Rng as Range Set RNG = Workbooks (“WorkbookName.xlsx”).Worksheets (“SheetName”).Cells (1,1)
How do you assign a range to a variable?
Dim Rng as Range Set Rng = Workbooks(“WorkbookName.xlsx”).Worksheets(“SheetName”).Range(“A1:D4”)
How do you select range by address?
Range(“A1:D4”).Select
How do you select cells by row and column?
Cells(1,1).Select
How do you change the value of a range by address?
Range(“A1:D4”).Value = “Text Here”
Using the cells function, select A1 in 2 ways
Cells(1,1).Select Cells(1,”A”).Select
How do you select the Row and Column of the active cell?
Cells(ActiveCell.Row,ActiveCell.Column)
How do you move the cursor to row 1 but stay in the same column?
Cells(1,ActiveCell.Column).Select
How do you select column D
Columns(“D”).Select
How do you select rows 4 and 9
Range(“4:4,9:9”).Select
How do you get to the bottom of a range?
Selection.End(x1Down).Select
Give an example of how to use a message box to make a decision about whether to delete or not?
Sub CellNavigation() If MsgBox(“Delete data?”, vbYesNo) = vbYes _ Then Cells.Delete MsgBox “Data Deleted” Else MsgBox “Data Not Deleted” End If End Sub
What are the VBA Message Box Buttons available?
vbOKOnly displays OK vbOKCancel displays OK - Cancel vbAbortRetryIgnor vbYesNoCancel vbYesNo vbRetryCancel
What is the vb value of the Cancel button on the Message Box
vbCancel
How would you describe the difference between the active cell and the selected cell?
many cells can be selected, but only one may be the active cell at any given time
If the active cell is A1, what cell will ActiveCell.Offset(1,2) select
C2