Excel VBA Excel VBA 2 — Questions and Answers
Question 1: Which VBA statement exits a For...Next loop immediately?
- Exit For (Correct answer)
- Break
- End For
- Stop
Correct answer: Exit For
Exit For immediately transfers control to the statement following the Next keyword.
Question 2: What does the UBound function return when called on an array?
- The highest valid index (Correct answer)
- The total number of elements
- The lowest valid index
- The data type of the array
Correct answer: The highest valid index
UBound returns the largest available subscript (upper bound) for the specified dimension of an array.
Question 3: Which method is used to add a new worksheet to a workbook in VBA?
- Worksheets.Add (Correct answer)
- Worksheets.New
- Sheets.Insert
- Workbook.AddSheet
Correct answer: Worksheets.Add
Worksheets.Add (or Sheets.Add) creates and returns a new worksheet in the workbook.
Question 4: What is the result of the VBA expression: 10 Mod 3?
- 1 (Correct answer)
- 3
- 0
- 3.33
Correct answer: 1
The Mod operator returns the remainder after division; 10 divided by 3 leaves a remainder of 1.
Question 5: Which VBA function converts a string to all uppercase letters?
- UCase (Correct answer)
- Upper
- ToUpper
- StrUpper
Correct answer: UCase
UCase(string) returns the string with all alphabetic characters converted to uppercase.
Question 6: How do you declare a variable that can hold any data type in VBA?
- Dim x As Variant (Correct answer)
- Dim x As Any
- Dim x As Object
- Dim x As Dynamic
Correct answer: Dim x As Variant
The Variant data type can store any kind of data including numbers, strings, dates, and objects.
Question 7: What does the VBA keyword 'Nothing' represent?
- An uninitialized or released object reference (Correct answer)
- A null numeric value
- An empty string
- A Boolean False equivalent
Correct answer: An uninitialized or released object reference
Nothing is used with object variables to indicate the variable holds no valid object reference.
Which VBA statement exits a For...Next loop immediately?