Excel test prep · Lesson 7 of 9
Excel VBA test: 25 practice questions with answers
Practice Excel VBA test for interviews: 25 questions on loops, ranges, Cells and Offset, strings, arrays, error handling, events and speed, explained.
Roles that mention automation, macros or "advanced Excel including VBA" often add a VBA section to the Excel test. The questions are usually short: read a few lines of code and say what they do, spot the bug, or choose the right object or property. This practice test has 25 such questions. Allow about 35 minutes.
All code is standard VBA for Excel on Windows and Mac. To check an answer, open the editor with Alt+F11 (Opt+F11 on Mac), insert a module and run the code with F5. If VBA is new to you, start with What is VBA? Macros, when to use them and macro security, Your first macro: Sub, MsgBox, running it and a button and Variables and data types: Dim, Option Explicit and scope; the explanations below link to the matching lessons.
Part 1: Basics and the object model
1. You recorded a macro you want to use in every workbook you open on your computer. Where should it be stored?
PERSONAL.XLSB opens hidden every time Excel starts, so its macros are always available. A macro stored in one workbook is available only while that file is open. See The macro recorder: record, read and clean up code.
2. Which file format keeps VBA code when you save a normal workbook?
Workbooks with macros must be saved as .xlsm (or the binary .xlsb). Saving as .xlsx silently removes all code. See The Developer tab and the VBA editor.
3. What does Option Explicit at the top of a module do?
Without it, a misspelled variable silently becomes a new, empty Variant. With it, VBA stops at compile time and highlights the unknown name. Turn it on for every new module in Tools > Options > Require Variable Declaration. See Variables and data types: Dim, Option Explicit and scope.
4. What does Debug.Print Cells(3, 2).Address print in the Immediate window?
Cells(row, column): row 3, column 2 is B3. .Address returns an absolute reference by default; .Address(False, False) gives B3. See Range and Cells: working with cells in VBA.
5. A1 holds the value 0.125 formatted as a percentage with one decimal. Which property returns the text 12.5% exactly as shown?
.Text returns what the cell displays (and returns #### if the column is too narrow). .Value and .Value2 return the number 0.125, and .Formula returns what was typed.
6. In the declaration Dim a, b As Long, what type is a?
Each variable needs its own As clause. Only b is Long; a gets the default type, Variant. Write Dim a As Long, b As Long. See Variables and data types: Dim, Option Explicit and scope.
Part 2: Ranges and loops
Questions 1, 2 and 4 below refer to these three listings.
Dim i As Long, total As Long
For i = 1 To 10 Step 3
total = total + i
Next i
MsgBox totalDim n As Long
n = 0
Do While n < 5
n = n + 2
Loop
Debug.Print nDim i As Long, lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If Cells(i, "A").Value = "Delete" Then Rows(i).Delete
Next i7. What does the message box in Listing 1 show?
With Step 3 the loop runs for i = 1, 4, 7 and 10 (10 is still within the limit), so total = 1 + 4 + 7 + 10 = 22. See For loops: For...Next and For Each.
8. What does Listing 2 print in the Immediate window?
The condition is checked at the top of each pass. n becomes 2, 4, then 6. At 6 the test n < 5 fails and the loop ends, so 6 is printed. See Do loops: Do While, Do Until and Exit Do.
9. What is the most reliable way to find the last used row in column A, even when the column has empty cells in the middle?
Starting at the very bottom of the sheet and jumping up finds the last non-empty cell. Jumping down from A1 stops at the first gap. Range("A:A").Count is simply the number of rows in the sheet. See Find the last row and build dynamic ranges in VBA.
10. Find the bug in Listing 3: it should delete every row whose column A says Delete, but when two such rows are next to each other, one survives. Why?
When row 5 is deleted, row 6 becomes row 5, but i moves on to 6. Writing For i = lastRow To 2 Step -1 avoids this because deletions only affect rows already checked. See For loops: For...Next and For Each.
11. What does Range("B2:D5").Cells.Count return?
B to D is 3 columns, rows 2 to 5 are 4 rows: 3 x 4 = 12 cells. .Rows.Count would return 4 and .Columns.Count 3.
12. What does Range("A1").Resize(2, 3).Address return?
Resize(rows, columns) keeps the top-left cell and makes the range 2 rows tall and 3 columns wide: A1:C2. See Range and Cells: working with cells in VBA.
13. What does Range("B2").Offset(1, -1).Address return?
Offset(rows, columns) moves 1 row down (to row 3) and 1 column left (to column A). Negative values move up or left.
Part 3: Strings, arrays and functions
Question 5 refers to Listing 4.
Function Tax(amount As Double) As Double
Tax = amount * 0.2
End Function14. What does Mid("Invoice-2026", 9, 4) return?
Mid(text, start, length) starts at character 9. Invoice has 7 characters and the hyphen is the 8th, so characters 9 to 12 are 2026. See Working with text (strings) in VBA.
15. What does InStr("data.xlsx", ".") return?
InStr returns the position of the first match, counting from 1. data has 4 letters, so the dot is character 5. It returns 0 when nothing is found.
16. What does UBound(Split("a,b,c", ",")) return?
Split always returns a zero-based array: a is element 0, b 1 and c 2. UBound gives the highest index, 2, even though there are 3 items. See Arrays in VBA: static, dynamic and fast range processing.
17. arr = Range("A1:C10").Value reads a block into a Variant. What does UBound(arr, 2) return?
Reading a range gives a 2-D array that starts at 1 in both dimensions: rows 1 to 10, columns 1 to 3. Dimension 2 is the columns, so its upper bound is 3. Working on such an array is much faster than reading cells one by one. See Arrays in VBA: static, dynamic and fast range processing.
18. Listing 4 is in a standard module. What does =Tax(150) return in a cell?
A public Function in a standard module is a user-defined function you can call from a cell. It returns whatever is assigned to its own name: 150 * 0.2 = 30. It would return #NAME? only if it were in a sheet module or the workbook had macros disabled. See User-defined functions (UDFs): your own worksheet functions.
19. Your code uses Application.WorksheetFunction.VLookup and the value is not found. What happens?
The WorksheetFunction version raises an error. Application.VLookup (without WorksheetFunction) instead returns an error value you can test with IsError, which is often more convenient. See Using Excel worksheet functions in VBA.
Part 4: Errors, events and speed
Question 1 refers to Listing 5.
Sub Test()
On Error GoTo Fail
Dim x As Long
x = 10 / 0
MsgBox "Done"
Exit Sub
Fail:
MsgBox "Error " & Err.Number
End Sub20. What message does Listing 5 show?
Dividing by zero raises run-time error 11. On Error GoTo Fail sends execution to the label, which displays Err.Number. The Exit Sub before the label stops the handler running when there is no error. See Error handling and debugging in VBA.
21. What happens when Dim x As Integer is followed by x = 40000?
An Integer holds -32,768 to 32,767, so 40000 does not fit. Use Long for whole numbers such as row numbers and counts; it is just as fast on modern computers. See Variables and data types: Dim, Option Explicit and scope.
22. What does Debug.Print 17 \ 5 & "-" & 17 Mod 5 print?
\ is integer division (17 \ 5 = 3) and Mod returns the remainder (2). Both are calculated before & joins the parts as text. See Operators and expressions in VBA.
23. Why is On Error Resume Next at the top of a long macro risky?
Use it only around the one line that may fail, check Err.Number, then switch back with On Error GoTo 0. See Error handling and debugging in VBA.
24. A Worksheet_Change event writes a value into the same sheet, and Excel freezes. What is the standard fix?
Writing to the sheet fires Worksheet_Change again, which writes again, and so on. Turning events off while you write breaks the loop. Use an error handler so events are always switched back on. See Events: macros that run by themselves.
25. A macro writes 100,000 values one cell at a time and takes minutes. Which change helps most?
Each cell access crosses from VBA to Excel, which is slow. One write of an array is often 100 times faster. Turning off ScreenUpdating and setting calculation to manual while the macro runs also helps. See Make your macros fast: speed optimization in VBA.