Stage 1 Review

What You've Learned

  1. VBA fundamentals and the development environment
  2. Variables and data types
  3. Conditionals (If, Select Case)
  4. Loops (For, Do While)
  5. Procedures and functions
  6. Introduction to the object model
  7. Error handling
  8. 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

vba
Dim x As Integer Dim name As String

Conditionals

vba
If condition Then ' code End If

Loops

vba
For i = 1 To 10 ' code Next i

Working with cells

vba
Range("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.

vba
Sub 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 Sub

Enter 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

  1. Writing simple programs on your own — reinforce the basics by finishing the day's exercises.
  2. Understanding objects, properties, and methods — make sure the Excel object model hierarchy is clear.
  3. Handling basic errors — get comfortable with the On Error statement forms.
  4. Using the debugging tools — become fluent with F8 Step Into and with watching variables.

Common Errors and Troubleshooting

  1. Run-time error '13': Type mismatch — clicking Cancel on InputBox returns an empty string, which blows up when assigned to a Double; test first with If str = "" Then Exit Sub.
  2. Run-time error '11': Division by zero — the divisor is 0; check it before calculating.
  3. Run-time error '424': Object required — a Set was omitted in an object assignment.
  4. 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.
  5. Compile error: Expected: end of statement — code copied from a web page brought along curly quotes; VBA only accepts straight double quotes.

Extension Exercises

  1. Add modulo (Mod) and exponentiation (^) to the mini calculator, and validate input with IsNumeric.
  2. 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 KnowledgeQuiz in the .bas file).
  3. Capstone project: add input validation and error handling to the Day09 price list generator, and save it as your personal template.

由在线工具箱(www.vba.net)整理制作