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.

Advanced 35 min read

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?

2. Which file format keeps VBA code when you save a normal workbook?

3. What does Option Explicit at the top of a module do?

4. What does Debug.Print Cells(3, 2).Address print in the Immediate window?

5. A1 holds the value 0.125 formatted as a percentage with one decimal. Which property returns the text 12.5% exactly as shown?

6. In the declaration Dim a, b As Long, what type is a?

Part 2: Ranges and loops

Questions 1, 2 and 4 below refer to these three listings.

VBA
Dim i As Long, total As Long
For i = 1 To 10 Step 3
    total = total + i
Next i
MsgBox total
Listing 1
VBA
Dim n As Long
n = 0
Do While n < 5
    n = n + 2
Loop
Debug.Print n
Listing 2
VBA
Dim 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 i
Listing 3: meant to delete every row marked Delete

7. What does the message box in Listing 1 show?

8. What does Listing 2 print in the Immediate window?

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?

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?

11. What does Range("B2:D5").Cells.Count return?

12. What does Range("A1").Resize(2, 3).Address return?

13. What does Range("B2").Offset(1, -1).Address return?

Part 3: Strings, arrays and functions

Question 5 refers to Listing 4.

VBA
Function Tax(amount As Double) As Double
    Tax = amount * 0.2
End Function
Listing 4

14. What does Mid("Invoice-2026", 9, 4) return?

15. What does InStr("data.xlsx", ".") return?

16. What does UBound(Split("a,b,c", ",")) return?

17. arr = Range("A1:C10").Value reads a block into a Variant. What does UBound(arr, 2) return?

18. Listing 4 is in a standard module. What does =Tax(150) return in a cell?

19. Your code uses Application.WorksheetFunction.VLookup and the value is not found. What happens?

Part 4: Errors, events and speed

Question 1 refers to Listing 5.

VBA
Sub Test()
    On Error GoTo Fail
    Dim x As Long
    x = 10 / 0
    MsgBox "Done"
    Exit Sub
Fail:
    MsgBox "Error " & Err.Number
End Sub
Listing 5

20. What message does Listing 5 show?

21. What happens when Dim x As Integer is followed by x = 40000?

22. What does Debug.Print 17 \ 5 & "-" & 17 Mod 5 print?

23. Why is On Error Resume Next at the top of a long macro risky?

24. A Worksheet_Change event writes a value into the same sheet, and Excel freezes. What is the standard fix?

25. A macro writes 100,000 values one cell at a time and takes minutes. Which change helps most?