Today's Goals
- Understand what VBA is
- Get comfortable with the Visual Basic Editor (VBE)
- Record and run macros
- Write your first VBA program
Key Concepts
1. What Is VBA?
VBA (Visual Basic for Applications) is the macro language Microsoft built into Office. It automates repetitive work in Excel, Word, and other apps: anything done manually more than once — filling forms, consolidating, copy-pasting, reformatting — can usually become a one-click VBA job. Hand-written VBA beats the macro recorder because you can add decisions and loops.
2. Opening the VBE
- Shortcut:
Alt + F11 - Or: Developer tab → Visual Basic
The Project Explorer on the left lists worksheets and modules; double-click a module to open its code window. No Developer tab? Enable it under File → Options → Customize Ribbon.
3. Inserting a Module and Recording Macros
Before writing code, right-click in the Project Explorer → Insert → Module. Record macros from Developer → Record Macro. Macro names take no spaces or special characters and no leading digit; Chinese characters are allowed. When a statement stumps you, record it once and study the generated code — the most practical way to teach yourself VBA.
4. Ways to Run a Macro
- In the VBE, click inside the macro and press
F5 - Press
Alt + F8and pick the macro - Bind it to a button
Note: a workbook containing macros must be saved as .xlsm, or the code is lost.
Today's Code
vbaSub HelloWorld()
MsgBox "Hello, VBA!"
End SubMsgBox shows the quoted text in a dialog. The .bas version, MsgBox "Hello, VBA!", vbInformation, "欢迎", also sets an icon and a caption.
Real-World Scenario
The setup: an office admin types a title and today's date into A1 of a sign-in sheet every day — twenty-plus repeats a month. Make it one click:
vbaSub 生成签到表抬头()
Range("A1").Value = "XX公司签到表"
Range("A2").Value = "日期: " & Date
MsgBox "抬头已生成", vbInformation, "完成"
End SubA1 gets the title; A2 reads "日期: 2026/10/11" — Date takes the system date, so it refreshes on every run. Bind the macro to a button via Developer → Insert → Button. That is VBA's core value: a daily routine frozen into one click.
Exercises
- Record a macro that sets cell A1 to "Hello"
- Edit the recorded macro and add more actions
- Create a macro that shows a welcome message with MsgBox
Exercise Solutions
- Developer → Record Macro → do the steps → Stop Recording, then edit the result in the VBE. The generated code looks like
Range("A1").SelectandActiveCell.FormulaR1C1 = "Hello". - Add statements in the VBE —
Range("A1").Font.Bold = Truebolds A1. - Write a fresh macro that calls MsgBox, e.g.
MsgBox "欢迎使用本系统!".
Extension Exercises
- Modify HelloWorld to show the current date and time with
Now. - Collect a name with
InputBox("请输入你的名字")and greet the person in a MsgBox.
Common Errors and Troubleshooting
| Error / Symptom | Cause | Fix |
|---|---|---|
Compile error: Expected: expression |
Missing quotes around a string | Wrap the text in double quotes |
| "Macros cannot be saved in a macro-free workbook" | Saved as .xlsx | Save As .xlsm |
Sub or Function not defined |
Misspelled name, or a call to a nonexistent macro | Check the spelling; Alt+F8 lists all macros |
| "Macros have been disabled" on open | Excel security | Click "Enable Content" |
Summary
Day01 answered three beginner questions: what VBA is, where code lives (a module in the VBE), and how to run it (F5, Alt+F8, a button). Remember two things: save as .xlsm, and read the error message before guessing.
由在线工具箱(www.vba.net)整理制作