VBA Flashcards

1
Q

What is a programming language?

A
  1. A system of statements used to control a computer.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is required of a programming language for it to be able to be manipulated a machine?

A

It needs to:

  1. Accept input: from external data or from the user;
  2. Store data;
  3. Manipulate data i.e. transform it;
  4. Produce output.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Visualise the Object-Oriented Programming paradigm.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

List the top 3 levels (from the top down) of the Excel Object Model.

A
  1. Application Object;
  2. Workbook Object;
  3. Worksheet Object

Everything in the Excel program (down to individual cells) is an object (within another object and/or containing other objects).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some properties, methods and events of the Application Object?

A
  • Properties: Version; Caption; Default file path; Workbook collection…
  • Methods: Calculate; CheckSpelling; Quit…
  • Events: SheetCalculate; NewWorkbook; WorkbookBeforeClose…
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some properties, methods and events of the Workbook Object?

A
  • Properties: Name; Sheets (collection); Saved…
  • Methods: Close; PrintOut; Save…
  • Events: BeforeClose; NewSheet; BeforeSave…
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are some properties, methods and events of the Worksheet Object?

A
  • Properties: Name; UsedRange; Visible…
  • Methods: Activate; Copy; Delete…
  • Events: Activate; Deactivate; Change…
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

On which tab can the “Macros” dialogue box be found?

A

View

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s the keyboard shortcut that changes between the VBA editor and the Excel workbook?

A

Alt+F11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the keyboard shortcut that runs the code after you mouse-click inside a Sub?

A

F5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s the keyboard shortcut that opens the Project Explorer pane?

A

Ctrl+R

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s the keyboard shortcut that opens the Excel Object Browser?

A

F2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In the VBA Object Browser, what image appears besides a property?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In the VBA Object Browser, what image appears beside an action?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In the VBA Object Browser, what image appears beside an event?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s best practice with regards to the number of workbooks open while using VBA?

A

One workbook open at a time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Where can the VBA Project Properties dialogue box be found? What can you do with it?

A

In the Tools tab of the VBA editor.

Here you can change the name of the project and add a discription to it.

You can also set a password protection of your code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Where can you protect visibility of your VBA code from others?

A

Tools > VBAProject - Project Properties > Protection

Make sure you don’t forget your password.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What’s the keyboard shotcut to close an excel file?

A

Ctrl+W

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What’s the keyboard shotcut to open an excel file?

A

Ctrl+O

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What’s the keyboard shotcut to save an excel file?

A

Ctrl+S

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What are the three types of module in VBA?

A

Object modue;

Standard module;

and

Class module;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How can the code of a module be exported?

A

Right-click a module, select “export file” and then save as either a .bas (basic file) or a .txt (simple text file).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are the two main types of procedure you can create in VBA?

A

Sub routines;

and

Functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are the differences between a Sub Routine and a Function?

A

Sub Routines can manipulate information in objects and perform calculations but cannot be part of formulas.

Functions can be used in formulas e.g. SUM or SUMIF.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Visualize a Sub Routine called Commission that assignes the value in cell C7 to a variable called Sale and the multiplies this by the value of a variable called Rate.

Store the result in a variable called Commission and produce a message box with the value.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

When you create a function in VBA, how do you assign the result of a function?

A

You assign the name of the result of the function to the name of the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

“Visualize a Sub Routine called Commission that assignes the value in cell C7 to a variable called Sale and the multiplies this by the value of a variable called Rate.

Store the result in a variable called Commission and produce a message box with the value.”

State the limitation of the above Sub Routine and visualise a Function that does the same.

A

You can only use the Sub Routine for a few variables at a time by using loops.

It is more efficient to use a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How do you decide between the use of a function and Sub Routine?

A

If the procedure is to be used in a formula, use a Function; if not, use a Sub Routine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

When code is written under the “Modules” folder or the “This Workbook” code module in the VBA Project pane, what’s the “Excel” assumption?

A

Excel assumes that it is available to any open workbook.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What’s the keyboad shortcut to create a new workbook when on is already open?

A

Ctrl+N

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

How is a procedure made only available to the workbook in which it is created?

A

By putting the word “Private” before the Sub Routine or Function declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What will “Sheet” and “This Workbook”-level modules usually be used for?

Where will normal Sub Routines ussually go?

A

Event handling procedures only.

The rest of the procedures usually go under the Standard modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What type of procedures cannot be put inside Sheet-level modules?

A

Functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

How are comments added in VBA?

What’s best practice with regards to the frequency of comments?

A

By typing and apostrophe ( ‘ ) before a sentence.

Comment often and clearly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

How can macros be ran through shape-buttons?

A

Insert tab > Illustrations > Shapes

Select a shape (and label it) and then right-click it and select “Assign Macro…”.

Select the module you want to assign to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How do you add a Macro button to the quick-access tool bar?

A

File > Options > Quick Access Tool Bar > (Choose Commands From) Macros (Select Macro) > Add

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

How do you add a macro to the Ribbon?

A

Select the tab you wish for it to be in and…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What is a good shortcut technique to writting VBA code?

A

Record the macro then lapidate the resulting code as you see fit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What are the 11 common VBA data types?

A

Byte: 0 to 255;

Boolean: True or False (0 or 1);

Integer: -32,768 to 32,767;

Long: -2.14 billion to 2.14 billion;

Single (six decimal places): very big range;

Double (fourteen decimal places): astronomical range;

Currency(4 decimal places): +/- 922 trilion;

Decimal(28 decimal places);

Date: from January 1st, 100 to December 31, 9999;

Object: An object reference (e.g. a worksheet or a range of cells);

Variant: allows any type of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Why should you use specific data types in VBA if the Variant data type is available?

A
  1. Other languages tend to be strongly typed so this is good practice of a tranferable skill;
  2. If you use the Variant (generic) data type, you will not benefit from type mismatch error messages and you may not have an anchor from which to investigate why the code is not working.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

How are variables declared in VBA?

A

By using the keyword “Dim” (for Dimension) and the variable type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

How do you make it a requirement that variables are declared before they are initialised?

A

By using the key-command “Option Explicit” at the top of your module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What is the correct way to declare multiple variables on the same line?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What is the default scope of variables declared inside Sub Routines for Functions?

A

They have localized scope i.e. they only exist inside that Sub Routine or Function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

How are variables made global i.e. accessible to all Sub Routines and Functions in a code module?

What can’t you do to it?

A

Declare it above all Sub Routines and Functions.

You can’t assign a value to it outside Sub Routines or Functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

How and where are constant “variables” declared?

A

Use the keyword “Const” and declare-assign it at the top of the module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What happens to a standard variable after you run it and reset?

How to you rectify that?

What are the limitations of static variables in VBA?

A

It defaults back to 0.

Create a static variable:

In stead of using the keyword “Dim” use “Static”.

The limitations are that the cumulative values are only retained while the workbook is open.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

What is the VBA symbol for integer division?

what does it do?

A

\

It divides numbers and eliminates any decimal places from the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What is the modulo operator in VBA?

A

Is is the “Mod” keyword rather than a symbol.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What symbol is used to concatenate messages with different data types?

A

&

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

How is a 1-dimention array declared in VBA?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

How is an array with assigned values declared in VBA?

What data type must be assigned to it?

A

Variant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

How is a 2-dimension array declared in VBA?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What is a dynamic array?

A

An array that can be created and thenre-dimenssioned as you see fit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

How is a dynamic array declared and manipulated?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

What happens when you re-dimension an array?

How do you prevent that?

A

The existing values are erased.

Use the the “Preserve” keyword after “ReDim” and before the variable name.

58
Q

In VBA, what are object variables? Give examples.

A

Variables that represent objects in the Excel Object Model e.g. cells, cell ranges, worksheets or even workbooks.

59
Q

How do built-in colour names have in common in VBA?

A

They start with the letters “vb”

60
Q

How can you streamline code referencing in VBA?

A

Use “With… End With” statements.

61
Q

Give an example of a circumstance in which you’d use a code loop in VBA?

A

When dealing with cell ranges or arrays.

62
Q

Visualize code that assigns the values in column C to an array and then display them in successive message box prompts.

A
63
Q

How can you skip values in an array?

A

Use the “Step” keyword.

64
Q

How you skip values backwards?

A
65
Q

What data type must be assigned to a variable that is being used to step through a “For Each” loop?

A

Variant

66
Q

Visualise VBA code that defines 2 variables as workbook and worksheet and then appends the word “test” to the end of each worksheet name.

A
67
Q

Which loop is used in VBA when you want code to run until a specific condition is met?

A

The “Do…Loop”.

68
Q

Visualise code that uses a standard “Do…Loop” to run until it finds an empty cell down column A.

A
69
Q

Visualise code that uses a “Do…Loop Until” to run until it finds an empty cell down column A.

A
70
Q

Visualise code that uses a “Do…Loop While” to run until it finds an empty cell down column A.

A
71
Q

How you you step through code line-by-line (“debug”)?

A

Press F8

72
Q

How do you stop an infinite loop?

A

Press Esc or Ctrl+C

73
Q

What does the “IIf” funtion do in VBA code?

A

It mimicks the very same funtionality and format as the “If” function in Excel.

74
Q

Visualise a simple “If…Then” code line in VBA.

A
75
Q

Visualise an “If…Then…Else” code line in VBA.

A
76
Q

Visualise an “If…Then…ElseIf” code line in VBA.

A
77
Q

Visualize a Case statement in VBA.

A
78
Q

What happens when there is an error related to data types?

A
79
Q

What is the default error-handling? In what situation would you use it?

A

“On Error GoTo 0”

Only used if we intend to change from another error-handling mode back to the default one.

80
Q

How do error handlers work? What happens if there is no error?

A

If there is no error the code will run until it reaches the “Exit Sub” line.

81
Q

What does the “On Error Resume Next” line do? What are the issues to be weary of?

A

It stipulates that if an error is found, the code should keep running as best it can.

The issue is that this means that no error message will pop up.

82
Q

What will the below code return?

A
83
Q

How can other Sub Routines be called inside a Sub Routine?

A
84
Q

If you’re calling Subs inside other Subs, what needs to be done in relation to variables that will be used by all these Subs?

A

Make the variables global.

85
Q

How do you step over code while “debug-stepping”?

How do you step out?

A

Shift+F8

Ctrl+Shift+F8

86
Q

How are breakpoints set up in VBA?

A

Click on the bar to the left of the code line.

87
Q

Why use breakpoints?

A

To help debug your program.

88
Q

What is the “Immediate” window?

A

It’s a window inside which you can “play” with your code line by line and see what happens in certain situations.

The window will only run the current line on code. It can, however, run multiple commands if they are separated by a colon.

89
Q

What line of code can you use to find out what’s the current value of a variable?

A

Have the “Immediate” window open and use “Debug.Print variable-name” inside the Sub.

90
Q

What’s the keyboard shortcut to open the “Immediate” window?

A

Ctrl+G

91
Q

What does a “Watch” do?

A

It displays the value of a variable continuously.

92
Q

How do you add a “Watch”?

A

Debug > Add Watch… > Type the name of the variable

93
Q

What is the use of “Watch”?

A

It helps verify that the code produces the correct results and that it follows the correct path to generate those results.

94
Q

How do you write content into a single Excel cell using VBA code?

A
95
Q

How can you write code that simultaneously writes to all the cells in the table, below?

A
96
Q

In Excel, what’s the maximum number of active cells that can be had at one time?

A

1

97
Q

After you run VBA code, what happens if you press Ctrl+Z?

A

Nothing, as you cannot undo VBA code.

98
Q

Visualise code that selects a range and copies/cuts and pastes it to the active cell in another worksheet.

A
99
Q

What happens if you use VBA to find a value and it finds nothing?

A

It returns an error.

100
Q

Visualize code that finds a value that the user inputs.

A
101
Q

Visualize code that finds a value that the user inputs and also returns the address.

A
102
Q

What is good practice in relation to the structure of Excel Workbooks?

A

Every Workbook should have a consistent structure; with each data point positionally fixed in relation to all the others.

103
Q

What VBA function deals with distances between cells?

A

OFFSET

104
Q

What should be done before using the OFFSET function?

A

Determine the active cell.

105
Q

Visualize VBA code that finds the first blank line and adds Name, email and suggetion (from another worksheet) to that line.

A
106
Q

What is the main benefit of using the “Offset” function?

A

It allows the code to reference 1 active cell rather than having to activate multiple cells; this make the code run much faster.

107
Q

When using IF/CASE statements, which cases should be determined first?

A

The most restrictive cases should come before less restrictive ones.

108
Q

What sign is used to cocatenate?

A

&

109
Q

What is good practice in relation to user experience?

A

Provide feedback through message boxes (prompts) that explain the results or the expectations.

110
Q

Visualize 3 VBA functions: one that returns the first 4 digits of a string, one that returns 2 digits starting with the 5th, and one that returs the leftmost 3.

A
111
Q

How are Worksheets activated?

A

By name - Worksheets(“Sheet2”).Activate

or

By relative position - Worksheets(1).Activate - This is a relative reference so if the positions change, the workseet activation changes.

112
Q

In VBA code, what is the difference between “Sheets” and “Worksheets” objects?

A

Sheets is broader than Worksheets. Sheets can include any type of sheet - including chartsheets, macrosheets from older versions of Excel etc…

113
Q

Where does a new sheet appear when you use Sheet.Add?

How can you specify where you want the sheet to be added?

Sheets.Add After:= Worksheets(“Sheet2”)

A

To the left of the active sheet.

114
Q

How can you specify where you want the sheet to be added?

A

Use the “After” command -

Sheets.Add After:= Worksheets(“Sheet2”)

115
Q

How do you add a worksheet to the end of the sheets bar?

A

Sheets.Add After:=Worksheets(Sheets.Count)

116
Q

How can you use VBA to rename a worksheet from “Sheet3” to “Test”?

A

Worksheets(“Sheet3”).Name = “Test”

117
Q

How can you use VBA to delete a worksheet called?

A

Worksheets(Sheet1”).Delete

118
Q

Visualise VBA code that disables a “worksheet-delete” display alert and then turns it back on after the delete.

A
119
Q

How is a workbook open with VBA?

A
120
Q

How is a workbook activated with VBA?

A
121
Q

How is a workbook SavedAs with VBA?

A
122
Q

How is a workbook closed with VBA?

A

use the “.Close” method

123
Q

When using the Find method in VBA, which argument will search the entire worksheet?

A

LookAt:=xlWhole

124
Q

In VBA, how to you avoid screen flashing (screen updates)?

A

Below the variable declarations, code:

Application.ScreenUpdating = False

and

Application.ScreenUpdating = True ;

at the end of your code.

125
Q

In VBA, visualise a sub that uses Excel’s built-in functions to randomly selects a prize from an array of 6 elements and return a mesage box with that prize.

A
126
Q

What code will make this message box appear?

A
127
Q

How can you discover all the available message box interactive options available in VBA?

A
128
Q

Visualize VBA code that asks the user to enter the location of the cell to be calculated upon.

A
129
Q

What type of input box allows for cells to be selected with the mouse pointer?

A

Type:=8

130
Q

Visualize VBA code that calls 2 sub routines inside another sub routine.

A
131
Q

What reasons are there for calling sub routines inside other sub routines?

A
  1. To make the code cleaner (putting comments on the subsidiary sub routines)
  2. To have one main Sub routine that has access to subsidiary sub routines;
  3. Code is easier to maintain i.e. main code is in one place;
  4. More portable. One page, one code.
132
Q

Which built-in Excel function selects a random number between two specified numbers?

A

RandBetween

133
Q

What should be done when you want a event to be read only by a specific Workbook - as opposed to any open Workbook (which can read from any general Module)?

A

Declare the Sub as “Private”:

Private Sub …()

134
Q

Visualize code that prevents a workbook from closing unless a certain condition is met.

A
135
Q

Visualize code that runs automatically once a Workbook is open.

A
136
Q

Visualize the code that asks the user if they are sure they want to save the workbook.

A
137
Q

What must be the parameters when using the “Worksheet_Change” event handler?

A

Private Sub Worksheet_Change(ByVal Target As Range)

138
Q

In vbOkCancel, what values are delivered for each choice?

A

Ok = 1

Cancel = 2

139
Q

Visualize VBA code that asks the user if they are sure they want to perform a change to a worksheet. If not, the code undoes the change and returns a message saying that the change was undone.

A
140
Q

What must always accompany a “Workbook_Activate()” event handler Sub?

A

A “Workbook_Deactivate()” event handler Sub.