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?

2. In an If/Else statement, when does the Else branch run?

3. What are the three steps for using a variable?

4. What does this line do?
NumberToAdmit = InputBox("How many students do you want to admit?")

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

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.

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

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.

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:

  1. Run it with data in A2 — you should get the InputBox and then the message
  2. 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:

  1. Navigates to the Applicant Information sheet and selects A2
  2. Uses a guard check — if A2 is empty, display "No applicants found." and the macro ends naturally through the If branch
  3. In the Else branch: asks for a group number via InputBox (Integer), then asks for a target admission count via a second InputBox (Integer)
  4. 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