A VBA procedure is a set of instructions written in Visual Basic for Applications that performs a specific task in Excel, like automating actions or calculations.
Click to reveal answer
beginner
How do you start writing a simple VBA procedure?
Open the VBA editor by pressing Alt + F11, insert a new module, and write a Sub procedure starting with Sub ProcedureName() and ending with End Sub.
Click to reveal answer
beginner
What does the following VBA code do?
Sub HelloWorld()
MsgBox "Hello, world!"
End Sub
This procedure shows a message box with the text "Hello, world!" when run.
Click to reveal answer
beginner
How do you run a VBA procedure in Excel?
You can run a VBA procedure by pressing F5 in the VBA editor or by assigning the procedure to a button or shortcut in Excel.
Click to reveal answer
intermediate
What is the difference between a Sub and a Function in VBA?
A Sub performs actions but does not return a value. A Function performs actions and returns a value that can be used in Excel formulas.
Click to reveal answer
Which keyword starts a VBA procedure that does not return a value?
ASub
BFunction
CDim
DSet
✗ Incorrect
A VBA procedure that does not return a value starts with the keyword Sub.
What does the MsgBox function do in VBA?
ASaves the workbook
BDisplays a message box to the user
COpens a new worksheet
DCalculates a sum
✗ Incorrect
MsgBox shows a popup message box with text or buttons.
How do you open the VBA editor in Excel?
APress F1
BPress Ctrl + C
CClick File > Save As
DPress Alt + F11
✗ Incorrect
Alt + F11 opens the VBA editor window.
Which statement correctly ends a VBA Sub procedure?
AEnd Sub
BStop
CExit Sub
DEnd Function
✗ Incorrect
Every Sub procedure must end with End Sub.
What is the main purpose of a VBA procedure?
ATo format cells manually
BTo enter data into cells
CTo automate tasks in Excel
DTo create charts
✗ Incorrect
VBA procedures automate repetitive or complex tasks.
Explain how to create and run a simple VBA procedure that shows a message box.
Think about the steps from opening the editor to seeing the message.
You got /4 concepts.
Describe the difference between a Sub and a Function in VBA.
Focus on what each procedure type can give back after running.
You got /4 concepts.
Practice
(1/5)
1. What does a simple VBA procedure in Excel always start with?
easy
A. Sub
B. Function
C. Begin
D. Start
Solution
Step 1: Understand VBA procedure structure
A simple VBA procedure always begins with the keyword Sub to define a subroutine.
Step 2: Confirm the correct starting keyword
The procedure does not start with Function, Begin, or Start, only Sub.
Final Answer:
Sub -> Option A
Quick Check:
Simple VBA procedures start with Sub [OK]
Hint: Remember VBA subs always start with 'Sub' keyword [OK]
Common Mistakes:
Confusing Sub with Function
Using Begin or Start instead of Sub
Forgetting to write Sub at the start
2. Which of the following is the correct way to end a simple VBA procedure?
easy
A. End Function
B. Stop
C. End Sub
D. Exit Sub
Solution
Step 1: Identify the correct ending for a Sub procedure
A simple VBA procedure defined with Sub must end with End Sub.
Step 2: Differentiate from other endings
End Function ends a function, Stop pauses code, and Exit Sub exits early but does not end the procedure.
Final Answer:
End Sub -> Option C
Quick Check:
Simple VBA procedures end with End Sub [OK]
Hint: End VBA subs with 'End Sub' exactly [OK]
Common Mistakes:
Using End Function for subs
Confusing Exit Sub with End Sub
Writing Stop instead of End Sub
3. What will the following VBA code do when run?
Sub ShowMessage()
MsgBox "Hello, Excel!"
End Sub
medium
A. Save the workbook automatically
B. Clear all cells in the worksheet
C. Change the background color of cells
D. Display a message box saying 'Hello, Excel!'
Solution
Step 1: Analyze the MsgBox command
The code uses MsgBox to show a popup message with the text "Hello, Excel!".
Step 2: Confirm no other actions are present
There are no commands to clear cells, change colors, or save the workbook.
Final Answer:
Display a message box saying 'Hello, Excel!' -> Option D
Quick Check:
MsgBox shows a popup message [OK]
Hint: MsgBox always shows a popup message [OK]
Common Mistakes:
Thinking MsgBox clears cells
Assuming MsgBox changes colors
Believing MsgBox saves files
4. Identify the error in this VBA procedure:
Sub ClearCells()
Range("A1:A10").ClearContents
End Function
medium
A. Procedure ends with End Function instead of End Sub
B. Missing parentheses in ClearContents
C. Range syntax is incorrect
D. Sub keyword is misspelled
Solution
Step 1: Check procedure start and end keywords
The procedure starts with Sub but incorrectly ends with End Function.
Step 2: Confirm correct ending for Sub procedures
Sub procedures must end with End Sub, not End Function.
Final Answer:
Procedure ends with End Function instead of End Sub -> Option A
Quick Check:
Sub procedures end with End Sub [OK]
Hint: Match Sub with End Sub, not End Function [OK]
Common Mistakes:
Ending Sub with End Function
Adding unnecessary parentheses to ClearContents
Incorrect Range syntax
Misspelling Sub keyword
5. You want to write a VBA procedure that changes the background color of cells A1 to A5 to yellow. Which code snippet correctly does this?
hard
A. Sub ColorCells()
Cells(1,1).Color = vbYellow
End Sub
B. Sub ColorCells()
Range("A1:A5").Interior.Color = vbYellow
End Sub
C. Sub ColorCells()
Range("A1:A5").Color = Yellow
End Sub
D. Sub ColorCells()
Range("A1:A5").Interior.ColorIndex = "Yellow"
End Sub
Solution
Step 1: Understand how to set cell background color in VBA
Use Range(...).Interior.Color with a color constant like vbYellow.
Step 2: Check each option for correct syntax
Sub ColorCells()
Range("A1:A5").Interior.Color = vbYellow
End Sub uses correct syntax. Sub ColorCells()
Range("A1:A5").Color = Yellow
End Sub uses .Color without Interior and 'Yellow' without vb prefix. Sub ColorCells()
Cells(1,1).Color = vbYellow
End Sub changes only one cell and uses .Color incorrectly. Sub ColorCells()
Range("A1:A5").Interior.ColorIndex = "Yellow"
End Sub uses ColorIndex with a string, which is invalid.
Final Answer:
Sub ColorCells()
Range("A1:A5").Interior.Color = vbYellow
End Sub -> Option B
Quick Check:
Use Interior.Color = vbYellow to color cells [OK]
Hint: Use Interior.Color = vbYellow to color cells [OK]
Common Mistakes:
Using .Color without Interior
Passing color name as string instead of constant
Changing only one cell instead of range
Using ColorIndex with string value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.