Adding Programming Concepts
Module 2 of 9
REMINDER: To fully understand this module, you should have already watched the Adding Programming Concepts video in Canvas and followed along with the Macro Demo file. This practice will build upon that foundation.
Beyond Recording
Recording captures actions — clicking, typing, navigating menus. But some things don't exist in Excel at all, which means they can't be recorded. They have to be typed. This module covers the two most important concepts you'll add by hand: If statements that let the macro make decisions, and variables that let it work with values that change each time it runs.
The workflow from here forward is always the same: record the parts you can, open the code, and add the typed logic around it. You're not writing everything from scratch — you're extending what the recorder gives you.
The better you know Excel, the better you'll be at macros. Macros are just automating what Excel can already do — plus the logic you add on top.
If Statements
An If statement lets your macro check a condition and do different things
depending on whether that condition is true or false. The recorder can capture
individual actions but it can't decide between them — that decision has to be
typed. The key words are If, Then, Else,
and End If.
Before writing the code, think through the logic in plain English first — just like writing pseudocode for an Excel formula. If you get the logic right in words, translating it to VBA is straightforward.
Pseudocode first
IF condition is true THEN
do this
ELSE
do that
END IF
Syntax
If condition Then
' code for true case
Else
' code for false case
End If
This example from the Macro Demo toggles notes on and off. The recorded code gave us both actions — turning notes on and turning them off. The If statement is the logic that decides which one to run:
Sub DisplayNotes()
If ActiveWorkbook.ShowNotesIndicators = True Then
ActiveWorkbook.ShowNotesIndicators = False
Else
ActiveWorkbook.ShowNotesIndicators = True
End If
End Sub
Notice that the code inside each branch came from recording — the If/Else structure is the only part that had to be typed. Record first, then add the logic around it.
The Guard Check Pattern
A guard check is an If statement placed at the top of a macro to verify that the data you need actually exists before the macro tries to do anything with it. If the table is empty, it shows a message and the macro stops naturally. If there is data, the macro continues through the Else branch and runs the rest of the code.
Pattern
IF first data cell is empty THEN
DISPLAY message — no data found
ELSE
run the rest of the macro
END IF
Here is what the guard check looks like in VBA. Notice that no special stop command is needed — when the If branch runs, the Else branch is skipped automatically:
Sheets("Applicant Information").Select
Range("A2").Select
If ActiveCell = "" Then
MsgBox "No applicants found. Please enter data first."
Else
' rest of macro goes here
End If
Variables
A variable stores a value that can change each time the macro runs. Instead of hardcoding a number into the code, you put the value in a variable — and the macro works correctly whether that number is 250 or 275 or anything else.
There are three steps every time you use a variable:
Three-step pattern
Step 1 — Declare it
Dim VariableName As DataType
Step 2 — Populate it
VariableName = InputBox("Prompt for user")
Step 3 — Use it
Replace the hardcoded value with the variable name
This example from the Macro Demo shows all three steps. The recorder hardcoded 250 into the Goal Seek. The variable replaces that hardcoded value so the user can enter a different number each time:
Sub GoalSeek_Admit()
' Step 1 — Declare
Dim NumberToAdmit As Integer
' Step 2 — Populate via InputBox
NumberToAdmit = InputBox("How many students do you want to admit?")
' Step 3 — Use (replaces the hardcoded 250 the recorder gave us)
Range("GPR_Accept").GoalSeek Goal:=NumberToAdmit, _
ChangingCell:=Range("Criteria_GPR")
End Sub
Named ranges make the code easier to read and more reliable. The recorder captured the cell address (L5) — after recording, replace it with the named range (GPR_Accept). If the layout of the worksheet ever changes, the named range updates automatically. A hardcoded cell reference does not.
InputBox
An InputBox pauses the macro and shows a dialog where the user can type something. Whatever they type is stored in the variable on the left side of the equals sign. This is how a macro becomes adaptable — you ask for the value at runtime instead of hardcoding it. InputBox is always typed, never recorded.
Syntax
VariableName = InputBox("Message to display to the user")
This example asks how many students to admit and stores the answer:
Dim NumberToAdmit As Integer
NumberToAdmit = InputBox("How many students do you want to admit?")
Quick Check
1. The macro recorder can capture an If statement — true or false?
The recorder only captures what you physically do in Excel — clicking, typing, navigating. Decisions (If/Then/Else) and values that change (variables) don't exist in Excel, so they can't be recorded. They always have to be typed.
2. In an If/Else statement, when does the Else branch run?
The If branch runs when the condition is true. The Else branch runs when it's false. Only one branch runs per execution — never both. This is what makes the toggle pattern work: if notes are on, turn them off; else turn them on.
3. What are the three steps for using a variable?
Every variable follows the same three steps: Dim to declare it and its data type, populate it with a value (via InputBox or assignment), then use it in place of the hardcoded value. Skip any step and the macro either errors or uses the wrong value.
4. What does this line do?NumberToAdmit = InputBox("How many students do you want to admit?")
InputBox pauses the macro and shows the prompt text in a dialog box. When the user types a value and clicks OK, that value is stored in the variable on the left side of the equals sign. This is step 2 — populating the variable.
5. In the guard check below, what happens when A2 is not empty?
If ActiveCell = "" Then
MsgBox "No applicants found."
Else
' rest of macro
End If
When the condition (ActiveCell = "") is false — meaning the cell
is not empty — the If branch is skipped entirely and execution continues
with the Else branch. The rest of the macro runs normally. No special
stop command is needed in the If branch.
Questions 3 and 4 show up together on the exam. Know the three-step variable pattern cold and you'll recognize it instantly in any macro, even ones you haven't seen before.
Easy Wins
Exercise 1 — Build the Toggle Macro Guided
Build the DisplayNotes toggle macro from the video. Record both actions first, then add the If/Else around the recorded code.
Open Excel. Start recording a macro — name it
DisplayNotesRecorded, store it in the Personal Workbook.
Go to Review → Notes → Show All Notes to turn notes on.
Then go back to Review → Notes → Show All Notes again to
turn them off. Stop recording. Press Alt+F11 to see
what was captured.
You should have two lines — one that turns notes on, one that turns them off. Those are the two branches of your If statement.
Before touching the code, write out the logic in plain English:
' IF notes are currently on THEN
' turn them off
' ELSE
' turn them on
' END IF
This is your blueprint. The recorded code goes inside the branches. The If/Then/Else/End If are the only new words you'll type.
Create a new Sub called DisplayNotes. Structure it like this,
placing the correct recorded line in each branch:
Sub DisplayNotes()
If ActiveWorkbook.ShowNotesIndicators = True Then
ActiveWorkbook.ShowNotesIndicators = False
Else
ActiveWorkbook.ShowNotesIndicators = True
End If
End Sub
The condition checks whether notes are currently showing. If they are, the True branch turns them off. If they aren't, the Else branch turns them on.
Put your cursor inside the DisplayNotes Sub and press
F8 to step through. Watch which branch executes based
on the current state of your notes. Run it again and watch it take the
other branch. Assign it to Ctrl+N and try toggling
from the worksheet.
Exercise 2 — Add a Variable to Goal Seek
Record the Goal Seek macro from the video. The recorder will hardcode the number you entered. Then apply the three-step variable pattern to make it ask the user each time.
What you need: a worksheet with a Goal Seek model. A simple version: put a formula in B1 that multiplies A1 by 10. Goal Seek sets B1 to a target by changing A1.
After recording, your code looks something like this:
Sub GoalSeek_Recorded()
Range("B1").GoalSeek Goal:=250, ChangingCell:=Range("A1")
End Sub
Add the three-step variable pattern to replace the hardcoded 250:
Sub GoalSeek_WithVariable()
Dim NumberToAdmit As Integer
NumberToAdmit = InputBox("How many students do you want to admit?")
Range("B1").GoalSeek Goal:=NumberToAdmit, ChangingCell:=Range("A1")
End Sub
The only change from the recorded version is: (1) the Dim line at the top, (2) the InputBox line to populate the variable, and (3) replacing the hardcoded number with the variable name. Run it twice with different numbers to confirm the variable is working.
The three-step pattern applied: Dim NumberToAdmit As Integer
declares it; NumberToAdmit = InputBox(...) populates it;
replacing 250 with NumberToAdmit uses it.
Each run asks for a fresh value — no hardcoded number in the code.
Practice Problem
Copy this data and paste it into cell A1 of a new Excel worksheet. Use Ctrl+V to paste — Excel will automatically split the columns. Rename the sheet Applicant Information to match the macro below.
| StudentID | LastName | FirstName | TAMU_GPR | Grade229 | Grade230 | Grade327 | FinalDecision |
|---|---|---|---|---|---|---|---|
| 724816395 | Anderson | Emma | 3.842 | A | B+ | A- | Accept |
| 831924750 | Martinez | Carlos | 3.156 | B | B | B- | Deny |
| 619283740 | Thompson | Sarah | 3.971 | A | A- | A | Accept |
| 748291036 | Nguyen | Michael | 3.624 | B+ | A- | B+ | Accept |
| 825163947 | Williams | Ashley | 2.987 | C+ | B | C | Deny |
| 736481920 | Brown | James | 3.745 | A- | B+ | A- | Accept |
| 814729360 | Davis | Lauren | 3.512 | B+ | B+ | B | Accept |
| 729183640 | Wilson | Tyler | 2.843 | C | C+ | B- | Deny |
| 836492710 | Johnson | Megan | 3.889 | A | A | A- | Accept |
| 715824930 | Garcia | Daniel | 3.234 | B | B- | B | Deny |
| 842163950 | Miller | Rachel | 3.763 | A- | A- | B+ | Accept |
| 726849130 | Moore | Kevin | 3.091 | B- | C+ | B | Deny |
| 819374620 | Taylor | Jessica | 3.956 | A | A- | A | Accept |
| 734826190 | Jackson | Ryan | 3.478 | B+ | B | B+ | Accept |
| 821649370 | White | Brittany | 2.765 | C | B- | C+ | Deny |
| 716293840 | Harris | Brandon | 3.834 | A- | A- | A- | Accept |
| 843716290 | Martin | Stephanie | 3.612 | B+ | B+ | A- | Accept |
| 728493610 | Thompson | Nathan | 3.147 | B | B- | B | Deny |
| 815264930 | Lewis | Amanda | 3.891 | A | A | A | Accept |
| 731846920 | Robinson | Justin | 3.423 | B+ | B | B | Accept |
| 824619730 | Clark | Samantha | 2.914 | C+ | B- | C | Deny |
| 719283460 | Rodriguez | Matthew | 3.756 | A- | B+ | A- | Accept |
| 836142790 | Lee | Kayla | 3.534 | B+ | A- | B+ | Accept |
| 724891360 | Walker | Andrew | 3.068 | B- | C+ | B- | Deny |
| 811364920 | Hall | Courtney | 3.847 | A | A- | A | Accept |
| 738261490 | Allen | Christopher | 3.389 | B | B+ | B | Accept |
| 826419730 | Young | Melissa | 2.831 | C | C+ | B- | Deny |
| 713849260 | Hernandez | Joshua | 3.912 | A | A | A- | Accept |
| 841263970 | King | Tiffany | 3.645 | B+ | A- | B+ | Accept |
| 727491360 | Wright | Patrick | 3.178 | B | B- | B | Deny |
Add a Guard Check and InputBox
Using the Aggie Advisors data, write a macro that checks whether the Applicant Information sheet has any records before doing anything else. If there is data, ask the user for a group number and confirm it.
What your macro needs to do:
- Navigate to the Applicant Information sheet and select cell A2
- Check if A2 is empty using an If statement
- If empty: display "No applicants found." — the macro ends here naturally
- If not empty (Else branch): ask for a group number via InputBox, store it in an Integer variable, and display: "Group [X] is ready to process" where X is the number entered
Test it both ways:
- Run it with data in A2 — you should get the InputBox and then the message
- Clear cell A2, run it again — you should get "No applicants found."
Expected results:
- With data: InputBox appears, then "Group [X] is ready to process"
- Without data: "No applicants found." and macro ends
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. Note that the exam questions will be more comprehensive than this single Challenge; this is just the first step to help build your foundation.
Complete Macro Setup
No hints. Exam level.
Write a macro from scratch that does all of the following:
- Navigates to the Applicant Information sheet and selects A2
- Uses a guard check — if A2 is empty, display "No applicants found." and the macro ends naturally through the If branch
- In the Else branch: asks for a group number via InputBox (Integer), then asks for a target admission count via a second InputBox (Integer)
- Displays a final MsgBox: "Group [X] — processing [Y] applicants" where X is the group number and Y is the admission count
Expected behavior:
- Empty table: "No applicants found." — macro ends
- Data present: two InputBoxes, then the summary MsgBox