Macro Foundations
Module 1 of 9
REMINDER: To fully understand Macro Foundations, you should have already watched the Macro Foundations Video in Canvas and followed along with the Macro Demo file. This practice will build upon that foundation.
What a Macro Is
A macro is a set of instructions that performs tasks in the order you specify. Instead of clicking through the same sequence of actions every time, you record or write those actions once and run them with a single click. Excel translates each action into Visual Basic for Applications (VBA) code that you can view, edit, and build on.
You create macros in two ways: recording actions in Excel, or typing code directly in the VBA Editor. Recording is how you start — it generates the basic structure automatically. Typing is how you extend it with logic that can't be recorded, like IF statements, variables, and loops. Most macros in this course use both.
"The recorder is your starting point, not your limitation. Record the actions, then open the code and understand what it generated. That's the workflow everything else builds on."
The VBA Editor
The VBA Editor is where your macro code lives. Press Alt+F11 to open it. You'll see a Project panel on the left showing your workbook and its modules — modules are where macros are stored. Double-click a module to see its code. The code window is where you read, write, and edit macros.
Keyboard Shortcuts
Alt+F11 → Open/close the VBA Editor F5 → Run the current macro F8 → Step through code one line at a time Alt+F11 again → Switch back to Excel
Every macro follows this basic structure:
Sub MacroName()
' Your recorded or typed code goes here
End Sub
Recorded vs Typed Code
Actions you perform in Excel — selecting cells, formatting, copying, navigating — can be recorded. Programming concepts that extend what Excel can do — IF statements, variables, loops, and InputBoxes — must be typed. Understanding which is which helps you know when to hit Record and when to open the editor and write.
What Gets Recorded vs What Gets Typed
Recorded (actions in Excel):
Range("A1").Select
Selection.Font.Bold = True
ActiveSheet.Name = "Report"
Typed (programming concepts):
If ActiveCell = "" Then ...
Dim Counter As Integer
Do Until ActiveCell = ""
InputBox("Enter a value")
Saving Your File
A regular Excel file (.xlsx) cannot save macros. When you close the file, any macros you recorded are gone. You must save as a Macro-Enabled Workbook (.xlsm) to keep your code. Excel will warn you if you try to save a file with macros as .xlsx — always choose to keep the macro-enabled format.
How to Save
File → Save As → Excel Macro-Enabled Workbook (.xlsm)
You cannot undo (Ctrl+Z) changes made by a macro. Before running a new or untested macro, save a backup of your file.
Quick Check
1. What keyboard shortcut opens the VBA Editor?
2. Which of the following must be TYPED and cannot be recorded?
3. You record a macro in an .xlsx file and close Excel. What happens to the macro?
4. You run a macro that accidentally deletes some data. You press Ctrl+Z. What happens?
5. Where are macros stored inside a workbook?
"Questions 3 and 4 are the ones students learn the hard way — losing a macro because they saved as .xlsx, or losing data because Ctrl+Z didn't work. Both are easy to avoid once you know."
Easy Wins
Exercise 1 — Record Your First Macro
Record a macro that types your name into cell A1 and bolds it. This is the simplest possible macro — the goal is just to see the whole process from recording to running to viewing the code.
Open Excel. Create a new blank workbook. Save it immediately as an .xlsm file: File → Save As → Excel Macro-Enabled Workbook. Name it whatever you like. If you skip this step and save as .xlsx later, your macro will be lost.
Go to the Developer tab → Record Macro. (If you don't see the Developer tab: File → Options → Customize Ribbon → check Developer → OK)
In the dialog box:
- Macro name: TypeMyName (no spaces allowed in macro names)
- Store macro in: This Workbook
- Click OK — recording has started
Click cell A1. Type your name. Press Enter. Click cell A1 again. Press Ctrl+B to bold it. That's it — keep it simple.
Go to Developer → Stop Recording. Your macro is now saved.
Press Alt+F11 to open the VBA Editor. In the Project panel, expand your workbook → Modules → Module1. Double-click it. You should see the code Excel generated.
Close the VBA Editor (Alt+F11). Click cell B1. Delete cell A1's content. Go to Developer → Macros → TypeMyName → Run. Watch what happens.
Complete Code (what you should see — yours may vary slightly):
Sub TypeMyName()
Range("A1").Select
ActiveCell.FormulaR1C1 = "Your Name"
Range("A1").Select
Selection.Font.Bold = True
End Sub
What to notice: Excel always records a Select first, then performs the action on the Selection. Later modules will show you how to clean this up — for now, just observe what the recorder generated.
Exercise 2 — Break It and Fix It
Open the macro you just recorded. Change the cell reference from
Range("A1") to Range("C3") in both places. Run it. What happens?
You're editing the hardcoded cell address. This is what "absolute reference" means in recorded code — it always goes to the same cell no matter where you are when you run it.
The macro now types your name in C3 and bolds it, regardless of which cell was selected before you ran it. The reference is hardcoded — it doesn't adapt to your position. This is the difference between absolute and relative references, which gets its own module later.
Practice Problem
Think of something repetitive you do in Excel — even something small. Record a macro that does it. Some ideas if you're not sure:
- Navigate to a specific sheet and select cell A1
- Bold the first row and autofit all columns
- Add today's date to a specific cell
- Clear the contents of a range
What your macro needs to do:
- Be recorded using Developer → Record Macro
- Be saved in This Workbook
- Actually run and do what you intended when you press Play
After recording:
- Open the VBA Editor and look at the code
- Find at least one line you can explain in plain English
- Find at least one line you're not sure about — note it down for when you get to later modules
There is no single correct answer here. The goal is to get comfortable with the record → view → run cycle before adding any complexity.
Challenge
These exercises are perfect preparation for the Sample Exam, which prepares you for the Macro Project Exam itself. Exam format may vary by semester.
Set Up the Student Information Sheet
No hints. Exam level.
Record a macro that does ALL of the following in one recording:
- Navigates to the Student Information sheet
- Bolds row 1 (the header row)
- Autofits all columns on Student Information
- Navigates back to the Applicant Information sheet
After recording, open the VBA Editor and clean up the code:
- Remove every line that contains
SelectorSelection - Rewrite the bold step so it acts directly on the row without selecting first — use
Rows("1:1").Font.Bold = True - Confirm the macro still runs correctly after your edits
Expected result: Row 1 of Student Information is bold. All columns are autofitted. Active sheet is Applicant Information.
See this in the Aggie Advisors project →