Home »
MCQs
VBA MCQs (Multiple-Choice Questions)
Practice these VBA MCQs (Multiple-Choice Questions) with answers and explanations to test your knowledge of Visual Basic for Applications, VBA programming concepts, variables, procedures, arrays, loops, error handling, Excel objects, ranges, worksheets, workbooks, events, UserForms, and automation.
VBA MCQs
These VBA multiple-choice questions cover fundamental and practical concepts used when developing macros and automating Microsoft Office applications, especially Microsoft Excel.
List of VBA MCQs
Here is a collection of VBA MCQs with answers and explanations for interview preparation, examinations, and practice.
1. What does VBA stand for?
- Visual Basic for Applications
- Visual Business Automation
- Virtual Basic Application
- Visual Basic Architecture
Answer: A) Visual Basic for Applications
Explanation:
VBA stands for Visual Basic for Applications. It is Microsoft's programming language for automating and extending Office applications such as Excel, Word, and Access.
2. Which VBA statement forces variables to be explicitly declared?
- Option Base
- Option Explicit
- Option Compare
- Option Private Module
Answer: B) Option Explicit
Explanation:
Option Explicit requires variables to be declared before they are used. This helps detect spelling mistakes and unintended undeclared variables.
3. Which statement correctly declares an Integer variable in VBA?
Integer count
Dim count As Integer
Declare count Integer
Var count As Integer
Answer: B) Dim count As Integer
Explanation:
The Dim statement declares a variable. The As Integer clause specifies its data type.
4. Which VBA data type is commonly used to store text?
- Boolean
- Date
- String
- Double
Answer: C) String
Explanation:
The String data type stores character and text data in VBA.
5. Which VBA data type can store True or False?
- Boolean
- Byte
- Integer
- Currency
Answer: A) Boolean
Explanation:
The Boolean data type represents two logical values: True and False.
6. Which keyword is used to assign an object reference to an object variable?
- Let
- Set
- Assign
- Object
Answer: B) Set
Explanation:
The Set statement assigns an object reference to an object variable, such as Set ws = Worksheets(1).
7. Which procedure type does not return a value directly to its caller?
- Function
- Property Get
- Sub
- Function returning Double
Answer: C) Sub
Explanation:
A Sub procedure performs actions but does not return a value through its procedure name. A Function can return a value.
8. Which is the correct declaration of a VBA Function that returns a Long value?
Function Total() As Long
Function Total Long()
Long Function Total()
Function As Long Total()
Answer: A) Function Total() As Long
Explanation:
A VBA Function specifies its return type after the parameter list using the As clause.
9. Which keyword is used to declare a constant in VBA?
- Static
- Const
- Fixed
- Constant
Answer: B) Const
Explanation:
The Const statement declares a named constant whose value cannot be changed during program execution.
10. What is the purpose of the VBA Variant data type?
- It can hold different kinds of values
- It can only store integers
- It can only store object references
- It is used only for arrays
Answer: A) It can hold different kinds of values
Explanation:
Variant is a flexible VBA data type that can contain different kinds of data, including numbers, strings, dates, and objects under appropriate conditions.
11. Which symbol begins a single-line comment in VBA?
- //
- #
- '
- --
Answer: C) '
Explanation:
An apostrophe starts a comment in VBA. The remainder of that line is treated as a comment and is not executed as VBA code.
12. Which VBA statement is used to conditionally execute code?
- If...Then
- Loop...Next
- With...End With
- Declare...End Declare
Answer: A) If...Then
Explanation:
If...Then evaluates a condition and executes code depending on whether the condition is true.
13. Which VBA structure is useful when comparing one expression against several possible values?
- Select Case
- For Each
- With
- Do Loop
Answer: A) Select Case
Explanation:
Select Case provides a convenient way to test one expression against multiple possible cases.
14. Which loop is specifically designed to iterate through each element of a collection or array?
- For Each...Next
- If...Then
- Select Case
- With...End With
Answer: A) For Each...Next
Explanation:
For Each...Next iterates through each element of an array, collection, or other supported enumerable object.
15. Which VBA loop executes while a condition remains True?
- Do While...Loop
- For...Next only
- Select Case
- With...End With
Answer: A) Do While...Loop
Explanation:
Do While...Loop continues executing its body while the specified condition evaluates to True.
16. What does the Exit For statement do?
- Restarts the loop
- Exits the current For loop
- Exits the entire VBA project
- Skips compilation
Answer: B) Exits the current For loop
Explanation:
Exit For immediately terminates the current For or For Each loop and continues execution after the loop.
17. Which operator is commonly used for string concatenation in VBA?
- +
- &
- *
- :
Answer: B) &
Explanation:
The & operator concatenates strings in VBA. For example, "Hello " & "World" produces "Hello World".
18. Which function returns the number of characters in a string?
- Count
- Length
- Len
- Size
Answer: C) Len
Explanation:
The VBA Len function returns the number of characters in a string.
19. Which VBA function returns characters from the beginning of a string?
- Left
- Start
- Begin
- First
Answer: A) Left
Explanation:
The Left function returns a specified number of characters from the left side of a string.
20. Which VBA function extracts characters beginning at a specified position in a string?
- Mid
- Part
- Slice
- Extract
Answer: A) Mid
Explanation:
The Mid function extracts a specified number of characters beginning at a specified position within a string.
21. Which VBA function converts a string to uppercase?
- Upper
- UCase
- ToUpper
- UpperCase
Answer: B) UCase
Explanation:
The UCase function returns a string converted to uppercase letters.
22. Which function can be used to remove leading and trailing spaces from a string?
- Clean
- Trim
- Strip
- RemoveSpace
Answer: B) Trim
Explanation:
The VBA Trim function removes leading and trailing spaces from a string.
23. Which function converts a numeric value or expression to a Long value?
- CLng
- CIntOnly
- LongValue
- ToLong
Answer: A) CLng
Explanation:
CLng converts an expression to the Long data type, subject to the normal VBA conversion rules.
24. Which VBA function is commonly used to display a dialog box containing a message?
- MsgBox
- DialogBox
- Message
- ShowDialog
Answer: A) MsgBox
Explanation:
The MsgBox function displays a message box and can also provide buttons and return information about the user's selection.
25. Which function is used to obtain input from the user through a simple dialog box?
- InputBox
- UserInput
- GetInput
- ReadBox
Answer: A) InputBox
Explanation:
The InputBox function displays a prompt and allows the user to enter a value.
26. Which statement is used to handle runtime errors using a designated error-handling procedure?
- On Error GoTo
- Handle Error
- Catch Error
- Error Handle
Answer: A) On Error GoTo
Explanation:
On Error GoTo enables a procedure to transfer control to a specified label when a runtime error occurs.
27. Which object represents a worksheet in the Excel VBA object model?
- Worksheet
- SheetObject
- ExcelPage
- WorkPage
Answer: A) Worksheet
Explanation:
The Worksheet object represents an individual worksheet in an Excel workbook.
28. Which object represents an Excel workbook?
- Workbook
- ExcelFile
- WorkFile
- BookObject
Answer: A) Workbook
Explanation:
The Workbook object represents an Excel workbook and provides access to its worksheets and other workbook-level members.
29. Which Excel VBA object represents a cell or range of cells?
- Range
- CellGroup
- CellSet
- DataArea
Answer: A) Range
Explanation:
The Range object represents a cell, a range of cells, or another supported range of cells in an Excel worksheet.
30. What does the following VBA statement do?
Worksheets("Sheet1").Range("A1").Value = 100
- Creates a new worksheet named Sheet1
- Places 100 into cell A1 of Sheet1
- Deletes the value from A1
- Formats A1 as currency
Answer: B) Places 100 into cell A1 of Sheet1
Explanation:
The statement accesses Sheet1, references cell A1 through the Range object, and assigns the value 100 to its Value property.
31. Which property returns the value stored in an Excel Range?
- Value
- Content
- Data
- TextValue
Answer: A) Value
Explanation:
The Value property gets or sets the value of a range. It is commonly used to read or write cell contents through VBA.
32. Which property returns the displayed text of a single Excel cell?
- Value
- Text
- DisplayValue
- Caption
Answer: B) Text
Explanation:
The Text property returns the formatted text for a cell as displayed, subject to the cell's width and formatting.
33. Which VBA statement activates a specific worksheet?
Worksheets("Sheet1").Activate
Worksheets("Sheet1").Open
Worksheets("Sheet1").Start
Worksheets("Sheet1").SelectPage
Answer: A) Worksheets("Sheet1").Activate
Explanation:
The Activate method makes the specified worksheet the active worksheet.
34. Which method saves the current Excel workbook?
- Save
- Store
- Write
- Commit
Answer: A) Save
Explanation:
The Save method saves changes made to a workbook.
35. Which method can be used to save an Excel workbook under a different file name or format?
- SaveAs
- SaveNew
- StoreAs
- ExportAsFile
Answer: A) SaveAs
Explanation:
The SaveAs method saves a workbook with a specified name, location, or file format.
36. Which method is used to add a new worksheet to an Excel workbook?
- Add
- InsertSheet
- NewSheet
- CreateWorksheet
Answer: A) Add
Explanation:
The Add method of the Worksheets collection can be used to create a new worksheet.
37. Which statement correctly deletes a worksheet object named Sheet2?
Worksheets("Sheet2").Delete
Worksheets("Sheet2").Remove
Worksheets("Sheet2").Erase
Worksheets("Sheet2").Clear
Answer: A) Worksheets("Sheet2").Delete
Explanation:
The Delete method removes the specified worksheet from the workbook. Excel may display a confirmation prompt depending on application settings.
38. Which Excel VBA property can be used to determine the last used row in a particular column by searching upward?
Cells(Rows.Count, 1).End(xlUp).Row
Cells(1, Rows.Count).LastRow
Rows.LastUsed(1)
Columns(1).EndRow
Answer: A) Cells(Rows.Count, 1).End(xlUp).Row
Explanation:
This pattern starts from the bottom of column A and uses End(xlUp) to move to the nearest populated cell above, with Row returning its row number.
39. Which VBA statement is used to execute a procedure from another procedure?
- Call
- RunOnly
- ExecuteProc
- InvokeSub
Answer: A) Call
Explanation:
The Call statement transfers control to a Sub, Function, or supported procedure. For a Sub, the Call keyword is optional in many cases.
40. What is the main purpose of the With...End With statement?
- To repeatedly reference the same object without writing its full expression
- To create a new object automatically
- To declare a global variable
- To handle runtime errors
Answer: A) To repeatedly reference the same object without writing its full expression
Explanation:
With...End With lets multiple statements operate on the same object without repeatedly specifying the complete object expression.
41. Which keyword is used to specify that a procedure parameter should receive a copy of the argument's value?
- ByRef
- ByVal
- CopyVal
- ValueRef
Answer: B) ByVal
Explanation:
ByVal passes an argument by value, whereas ByRef passes a reference to the variable, allowing the called procedure to modify the caller's variable under the applicable rules.
42. Which keyword can be used to declare a variable whose value persists between calls to a procedure?
- Static
- Persist
- Permanent
- Retain
Answer: A) Static
Explanation:
A local variable declared with Static retains its value between calls to the procedure in which it is declared.
43. Which statement declares a dynamic array that can later be resized?
Dim values() As Long
Dim values(10) As Long
Dynamic values As Long
Array values() As Long
Answer: A) Dim values() As Long
Explanation:
An array declared without bounds, such as Dim values() As Long, can later be dimensioned with ReDim.
44. Which statement resizes an array while preserving its existing elements?
ReDim Preserve
Resize Preserve
Preserve Array
ReSize Keep
Answer: A) ReDim Preserve
Explanation:
ReDim Preserve changes the size of a dynamic array while retaining its existing values, subject to VBA's array resizing rules.
45. Which Excel VBA event procedure commonly runs when a workbook is opened?
- Workbook_Open
- Workbook_Start
- Workbook_Load
- Workbook_Begin
Answer: A) Workbook_Open
Explanation:
Workbook_Open is a workbook event procedure that runs when the workbook is opened, provided events are enabled and the procedure is placed in the appropriate workbook module.
46. In Excel VBA, where is the Worksheet_Change event procedure normally placed?
- In the relevant worksheet module
- Only in a standard module
- Only in the ThisWorkbook module
- In the Immediate window
Answer: A) In the relevant worksheet module
Explanation:
The Worksheet_Change event belongs to a worksheet object and is normally written in the module for the worksheet whose changes should trigger the event.
47. Which VBA window is primarily used to execute individual VBA statements interactively while debugging?
- Immediate Window
- Properties Window
- Project Explorer
- Object Browser
Answer: A) Immediate Window
Explanation:
The Immediate Window in the Visual Basic Editor can be used to execute VBA statements interactively and inspect values while debugging code.
48. What is the purpose of a breakpoint in VBA debugging?
- To pause execution at a specified line
- To permanently delete a procedure
- To convert VBA into compiled machine code
- To prevent a workbook from opening
Answer: A) To pause execution at a specified line
Explanation:
A breakpoint pauses program execution at a selected line so that variables, object states, and program flow can be inspected during debugging.
49. What is the result of the following VBA code?
Dim total As Long
total = 10
If total > 5 Then
total = total + 20
End If
total becomes 5
total becomes 10
total becomes 30
- The code generates a syntax error
Answer: C) total becomes 30
Explanation:
The initial value of total is 10. Since 10 is greater than 5, the If block executes and adds 20, resulting in 30.
50. What does the following Excel VBA code do?
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
With ws.Range("A1:A5")
.Value = 10
.Font.Bold = True
End With
- Deletes A1:A5 and creates a new range
- Places 10 in A1:A5 and makes the cells bold
- Places 10 only in A1 and makes Sheet1 bold
- Creates five new worksheets and assigns each a value of 10
Answer: B) Places 10 in A1:A5 and makes the cells bold
Explanation:
The code assigns the value 10 to every cell in the range A1:A5. The With block then sets the Font.Bold property of that same range to True, making the five cells bold.
Advertisement
Advertisement