0
0
Excelspreadsheet~20 mins

Variables and loops in VBA in Excel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
VBA Variables and Loops Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📊 Formula Result
intermediate
2:00remaining
What is the output of this VBA loop?

Consider this VBA code inside a module:

Sub CountToFive()
  Dim i As Integer
  Dim result As String
  result = ""
  For i = 1 To 5
    result = result & i & ","
  Next i
  MsgBox result
End Sub

What will the message box show when this macro runs?

Excel
Sub CountToFive()
  Dim i As Integer
  Dim result As String
  result = ""
  For i = 1 To 5
    result = result & i & ","
  Next i
  MsgBox result
End Sub
A1,2,3,4,5,
B1,2,3,4,5
C12345,
DError: result variable not initialized
Attempts:
2 left
💡 Hint

Look at how the loop adds each number and a comma to the string.

Function Choice
intermediate
2:00remaining
Which VBA loop correctly sums numbers 1 to 10?

You want to add numbers from 1 to 10 in VBA and store the total in a variable named total. Which loop code correctly does this?

A
Dim i As Integer
Dim total As Integer
total = 0
For i = 1 To 10
total = total * i
Next i
B
Dim i As Integer
Dim total As Integer
total = 0
For i = 1 To 10
total = i
Next i
C
Dim i As Integer
Dim total As Integer
total = 0
For i = 1 To 10
total = total + i
Next i
D
Dim i As Integer
Dim total As Integer
total = 1
For i = 1 To 10
total = total + i
Next i
Attempts:
2 left
💡 Hint

Think about how to add each number to the total step by step.

data_analysis
advanced
2:00remaining
How many times does this VBA loop run?

Look at this VBA code:

Dim count As Integer
count = 0
Dim i As Integer
For i = 10 To 1 Step -2
  count = count + 1
Next i
MsgBox count

What number will the message box show?

Excel
Dim count As Integer
count = 0
Dim i As Integer
For i = 10 To 1 Step -2
  count = count + 1
Next i
MsgBox count
A4
B5
C6
DError: Step value must be positive
Attempts:
2 left
💡 Hint

Count how many numbers are in the sequence 10, 8, 6, 4, 2.

🎯 Scenario
advanced
2:00remaining
You want to exit a loop early in VBA. Which code does this?

You have a loop in VBA and want to stop it when a variable found becomes True. Which code snippet correctly exits the loop early?

A
For i = 1 To 10
  If found = True Then Stop
  ' other code
Next i
B
For i = 1 To 10
  If found = True Then Continue
  ' other code
Next i
C
For i = 1 To 10
  If found = True Then Break
  ' other code
Next i
D
For i = 1 To 10
  If found = True Then Exit For
  ' other code
Next i
Attempts:
2 left
💡 Hint

Think about the VBA keyword to exit a loop immediately.

🧠 Conceptual
expert
2:00remaining
What error occurs with this VBA variable declaration?

Consider this VBA code snippet:

Dim total As Integer
Dim total As String
total = 5

What happens when you run this code?

Excel
Dim total As Integer
Dim total As String
total = 5
ACompile error: Duplicate declaration of variable 'total'
BRuntime error: Type mismatch
CNo error, total is treated as String
DNo error, total is treated as Integer
Attempts:
2 left
💡 Hint

Can you declare the same variable name twice with different types in VBA?