Automate AP Cleaning + Flag Duplicates
What is a Macro?
A Macro is like a digital recorder for Excel. It watches your mouse clicks and keystrokes, memorizes them, and saves them as a small program (VBA). You can then "replay" those steps instantly on new data.
In this lab, you will record a macro to clean a dataset, apply audit logic to find duplicates, and build a one-click exception report tool.
Objectives
- Record a macro for formatting
- Automate Duplicate Detection
- Assign macros to buttons
- Understand basic VBA concepts
Environment Setup
File → Options → Customize Ribbon → Check box for "Developer" (on the right side).
File → Save As → Change file type to Excel Macro-Enabled Workbook (.xlsm)
Create a new blank sheet and rename it exactly Exceptions. Leave it empty for now.
Demo 1: The "Cleaning + Logic" Macro
We will record a macro named Clean_AP_Report. This will format the data AND automatically add our audit formula.
We will type the = sign manually during the macro recording.
Recording Checklist
Pro Tip: Don't click random cells! Follow exactly:
- Start Recording: Developer Tab → Record Macro. Name it
Clean_AP_Report. - Bold Headers: Select Row 1 → Home → Bold.
- Create Table: Click any single cell in the data (e.g., A1) → Press Ctrl + T → OK.
- Number Format: Select 'Amount' column (E) → Home → Number (or click Comma Style).
- Autofit: Select all columns (Ctrl+A) → Double click line between headers A and B.
- Freeze Panes: View → Freeze Panes → Freeze Top Row.
- Sort: Click inside table → Data → Sort (Vendor A-Z, Add Level: Amount Largest to Smallest).
- Create Logic Column: Click cell G1 → Type "Dup_Flag".
-
Paste Formula: Click G2 → Type
=→ Paste (Ctrl+V) → Enter. - Conditional Format: Select Column G → Home → Conditional Formatting → Highlight Cells → Text that Contains... "DUPLICATE" → OK.
- STOP RECORDING: Developer Tab → Stop Recording.
Undo will not work on macros! To test, you must delete your data and paste the original raw data back.
After pasting, go to Developer → Macros → Run.
Understanding the Logic
Great job! Your macro now automatically inserts a complex audit formula. Let's understand what that formula actually does.
- COUNTIFS: This counts rows where both criteria match.
- Criteria 1: Does the Vendor match this row's Vendor?
- Criteria 2: Does the Amount match this row's Amount?
- >1: If it finds more than 1 match (itself + another one), it's a duplicate.
Your Turn: The "Exception Report"
Challenge: Create Macro "Make_Exception_Report"
Start recording, then perform these EXACT steps.
Goal: Copy duplicates to the existing Exceptions sheet.
- Filter: Click the arrow on header
Dup_Flag. Uncheck "OK". Keep "DUPLICATE" checked. - Copy: Click inside the table. Press Ctrl + A twice to select the whole table. Press Ctrl + C.
- Go to Destination: Click on the Exceptions sheet tab.
- Select A1: Click cell A1.
- Paste Values: Right-click cell A1 → Select Paste Values (123 icon).
- Stop Recording: Developer → Stop Recording.
Go back to your AP_Raw sheet.
- Go to Developer → Insert → Button (Form Control).
- Draw the button near the top of your data.
- Select
Make_Exception_Reportfrom the list → OK. - Right-click the button → Edit Text → Rename to "Build Report".
The Real Test: Next Month's Data
The true power of a macro isn't cleaning this data—it's cleaning next month's data instantly.
- Reset: Delete ALL data in your
AP_Rawsheet. - Clear Exceptions: Go to the
Exceptionssheet and delete the old data (Select all & delete). Do NOT delete the sheet itself! - Get New Data: Click the button below to copy the "February Data".
- Paste: Paste it into cell A1 of
AP_Raw. - Run: Click your macro buttons (Clean & Build Report).
Did it Crash? (Debugging)
Common Error: "Subscript out of range"
If you get Run-time error '9', it usually means the macro is looking for a sheet that doesn't exist.
Did you delete the "Exceptions" sheet? The macro expects that sheet to be there waiting.
How to fix it:- Click End on the error popup.
- Create a new blank sheet.
- Rename it exactly: Exceptions
- Try pressing your button again.
Instructor Notes & Solutions
Key Teaching Lines
- "Recording is not smart — it records EXACT clicks. If you click cell A5, it always goes to A5."
- "In audit, automation = consistency. The macro doesn't get tired or make typos."
- "We automate the boring steps (cleaning) so we can focus on judgment (investigating duplicates)."
Exit Ticket Questions
- What does "Record Macro" actually do? (Translates clicks to VBA code)
- Why is .xlsm required? (Security/Macro storage)
- Name one accounting task you could automate? (Reconciliations, formatting monthly reports)