VBA Interview Questions and Answers
Ques 6. Explain the purpose of the 'With' statement in VBA.
The 'With' statement allows you to perform a series of actions on a specified object without repeating the object reference. It enhances code readability and can improve performance.
Example:
With Range("A1")
.Value = 42
.Font.Bold = True
End With
Ques 7. How do you loop through a range of cells in VBA?
You can use a 'For Each' loop to iterate through each cell in a range.
Example:
Dim cell As Range
For Each cell In Range("A1:A10")
Debug.Print cell.Value
Next cell
Ques 8. What is the purpose of the 'Option Explicit' statement?
'Option Explicit' forces explicit declaration of all variables and helps catch undeclared variables at compile-time.
Example:
'Option Explicit
Sub Example()
Dim x As Integer
y = 10 ' This will cause an error without 'Option Explicit'
End Sub
Ques 9. Explain the difference between 'ActiveCell' and 'Selection' in VBA.
'ActiveCell' refers to the currently selected cell, while 'Selection' refers to the currently selected range of cells.
Example:
ActiveCell.Value = "Hello"
Selection.Font.Bold = True
Ques 10. How do you create a UserForm in VBA?
You can create a UserForm by right-clicking on the VBA project, selecting 'Insert' -> 'UserForm', and then design the form using the toolbox.
Example:
Sub ShowUserForm()
UserForm1.Show
End Sub
Most helpful rated by users: