Stage 1 Review
What You've Learned
- VBA fundamentals and the development environment
- Variables and data types
- Conditionals (If, Select Case)
- Loops (For, Do While)
- Procedures and functions
- Introduction to the object model
- Error handling
- Debugging techniques
These 8 days form a complete VBA on-ramp: set up the environment and get Hello World running (Day01), pick up the four building blocks — variables, conditionals, loops, procedures (Day02-Day05) — then learn to drive Excel objects (Day06), contain run-time errors (Day07), and check your own code with breakpoint debugging (Day08). To gauge where you stand, run the KnowledgeQuiz procedure in the .bas file.
Handy Code Snippets
Declaring variables
vbaDim x As Integer
Dim name As StringConditionals
vbaIf condition Then
' code
End IfLoops
vbaFor i = 1 To 10
' code
Next iWorking with cells
vbaRange("A1").Value = "Hello"
Cells(1, 1).Value = "Hello"Real-World Scenario
A mini calculator makes a fitting finale: InputBox for input, Select Case for branching, and On Error as the net — the three main themes of Stage 1, each in its place.
vbaSub MiniCalculator()
Dim num1 As Double, num2 As Double
Dim result As Double, op As String
num1 = InputBox("Enter the first number:")
op = InputBox("Enter an operator (+,-,*,/):")
num2 = InputBox("Enter the second number:")
On Error GoTo ErrorHandler
Select Case op
Case "+": result = num1 + num2
Case "-": result = num1 - num2
Case "*": result = num1 * num2
Case "/": result = num1 / num2
Case Else
MsgBox "Invalid operator", vbExclamation
Exit Sub
End Select
MsgBox num1 & " " & op & " " & num2 & " = " & result
Exit Sub
ErrorHandler:
MsgBox "Calculation error: " & Err.Description, vbCritical
End SubEnter 10 and 0 with division and the divide-by-zero error fires — caught by ErrorHandler and reported cleanly. That is Day07's error handling doing its job in a real program. The full version lives in the .bas file.
Self-Check
- I can write simple VBA programs on my own
- I understand objects, properties, and methods
- I can handle basic errors
- I can use the debugging tools
Coming Up in Stage 2
Stage 2 covers Excel's core features: workbook/worksheet operations, cell manipulation, UserForms, and more.
Self-Check Answers
- Writing simple programs on your own — reinforce the basics by finishing the day's exercises.
- Understanding objects, properties, and methods — make sure the Excel object model hierarchy is clear.
- Handling basic errors — get comfortable with the On Error statement forms.
- Using the debugging tools — become fluent with F8 Step Into and with watching variables.
Common Errors and Troubleshooting
Run-time error '13': Type mismatch— clicking Cancel on InputBox returns an empty string, which blows up when assigned to a Double; test first withIf str = "" Then Exit Sub.Run-time error '11': Division by zero— the divisor is 0; check it before calculating.Run-time error '424': Object required— aSetwas omitted in an object assignment.- Pressing F5 does nothing — the cursor is not inside any Sub, or macro security settings have macros disabled; check Macro Settings and reopen the file.
Compile error: Expected: end of statement— code copied from a web page brought along curly quotes; VBA only accepts straight double quotes.
Extension Exercises
- Add modulo (
Mod) and exponentiation (^) to the mini calculator, and validate input withIsNumeric. - Build a self-quiz program: store 5 questions and answers in an array, ask and score them in a loop, and report the score with MsgBox (see
KnowledgeQuizin the .bas file). - Capstone project: add input validation and error handling to the Day09 price list generator, and save it as your personal template.
由在线工具箱(www.vba.net)整理制作