Excel VBA Excel VBA — 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?
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.
Question 8: Which VBA event fires when a cell's value is changed by the user on a worksheet?
- Worksheet_Change (Correct answer)
- Worksheet_Edit
- Worksheet_Update
- Worksheet_Modify
Correct answer: Worksheet_Change
The Worksheet_Change event fires whenever a user or external link changes a cell in the worksheet.
Question 9: What does Application.ScreenUpdating = False do in a VBA macro?
- Prevents the screen from refreshing while the macro runs (Correct answer)
- Hides the Excel window entirely
- Disables formula recalculation
- Turns off all events
Correct answer: Prevents the screen from refreshing while the macro runs
Setting ScreenUpdating to False stops the screen from flickering during macro execution, improving speed.
Question 10: Which function returns the number of characters in a string in VBA?
- Len (Correct answer)
- Count
- Size
- Length
Correct answer: Len
Len(string) returns the number of characters contained in the string argument.
Question 11: In VBA, what does the keyword 'Me' refer to inside a UserForm's code module?
- The UserForm itself (Correct answer)
- The active workbook
- The calling procedure
- The parent worksheet
Correct answer: The UserForm itself
Me refers to the instance of the class (UserForm, worksheet, or workbook) in whose code module it appears.
Question 12: What VBA construct handles errors that occur during runtime?
- On Error GoTo (Correct answer)
- Try...Catch
- Error...Handle
- Catch...Finally
Correct answer: On Error GoTo
VBA uses On Error GoTo [label] to redirect execution to an error-handling block when a runtime error occurs.
Question 13: Which property of a Range object returns the number of rows in that range?
- Rows.Count (Correct answer)
- RowCount
- CountRows
- Rows.Total
Correct answer: Rows.Count
Range.Rows.Count returns an integer representing how many rows are in the specified range.
Question 14: What is the VBA equivalent of Excel's VLOOKUP function when searching a sorted array?
- Application.WorksheetFunction.VLookup (Correct answer)
- VBA.VLookup
- Range.VLookup
- Excel.VLookup
Correct answer: Application.WorksheetFunction.VLookup
Application.WorksheetFunction.VLookup lets you call Excel's built-in VLOOKUP from within VBA code.
Question 15: Which VBA collection object contains all open workbooks in the current Excel instance?
- Workbooks (Correct answer)
- OpenFiles
- ExcelFiles
- ActiveWorkbooks
Correct answer: Workbooks
The Workbooks collection is a property of the Application object and holds all currently open Workbook objects.
Question 16: What does the MsgBox function return when the user clicks the Cancel button?
- vbCancel (2) (Correct answer)
- vbNo (7)
- False
- 0
Correct answer: vbCancel (2)
MsgBox returns the VBA constant vbCancel, which has an integer value of 2, when Cancel is clicked.
Question 17: Which statement is used to define a reusable block of code that does NOT return a value in VBA?
- Sub (Correct answer)
- Function
- Procedure
- Macro
Correct answer: Sub
A Sub procedure performs actions but does not return a value, unlike a Function which returns a result.
Question 18: How do you copy a range in VBA without including the clipboard (direct value copy)?
- destRange.Value = sourceRange.Value (Correct answer)
- sourceRange.Copy destRange
- sourceRange.Paste destRange
- destRange.Assign sourceRange
Correct answer: destRange.Value = sourceRange.Value
Assigning .Value directly between ranges transfers data without using the clipboard or triggering a copy animation.
Question 19: What does the Cells property return when called as Cells(2, 3) on a worksheet?
- The cell in row 2, column 3 (C2) (Correct answer)
- The cell at column 2, row 3 (B3)
- A range of 2 rows and 3 columns
- The 2nd cell in the 3rd row
Correct answer: The cell in row 2, column 3 (C2)
Cells(row, column) uses numeric row and column indices, so Cells(2,3) refers to row 2, column C — cell C2.
Question 20: Which VBA function extracts a substring from the right side of a string?
- Right (Correct answer)
- RightStr
- SubRight
- Extract
Correct answer: Right
Right(string, length) returns the specified number of characters from the rightmost end of the string.
Question 21: What happens when you use 'ReDim Preserve' on a dynamic array in VBA?
- The array is resized while keeping existing data (Correct answer)
- The array is cleared and resized
- The array dimensions are locked permanently
- An error is thrown if new size is smaller
Correct answer: The array is resized while keeping existing data
ReDim Preserve resizes a dynamic array and retains the values already stored in it, unlike plain ReDim which resets them.