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 SubWhat will the message box show when this macro runs?
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
Look at how the loop adds each number and a comma to the string.
The loop runs from 1 to 5. Each time it adds the number and a comma to the string. So the final string is "1,2,3,4,5,".
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?
Think about how to add each number to the total step by step.
Option C initializes total to 0 and adds each i from 1 to 10. This correctly sums the numbers.
Option C overwrites total each time, so only 10 remains.
Option C multiplies total by i, which calculates factorial, not sum.
Option C starts total at 1, so the sum is off by 1.
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?
Dim count As Integer count = 0 Dim i As Integer For i = 10 To 1 Step -2 count = count + 1 Next i MsgBox count
Count how many numbers are in the sequence 10, 8, 6, 4, 2.
The loop starts at 10 and decreases by 2 until it reaches 1 or less. The values are 10, 8, 6, 4, 2. That's 5 numbers, so count increments 5 times (option B).
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?
Think about the VBA keyword to exit a loop immediately.
In VBA, Exit For stops the loop immediately. There is no Continue or Break keyword in VBA loops. Stop pauses code execution for debugging.
Consider this VBA code snippet:
Dim total As Integer Dim total As String total = 5
What happens when you run this code?
Dim total As Integer
Dim total As String
total = 5Can you declare the same variable name twice with different types in VBA?
VBA does not allow declaring the same variable name twice in the same scope. This causes a compile error for duplicate declaration.