Exercise 1: Format Specimen Data
Time to apply what you've learned! This hands-on exercise will reinforce the recording and editing skills from the previous sections.
The Challenge
You have a raw data file with specimen measurements that needs professional formatting. Your task is to create a macro that automates this formatting.
Exercise 1: Format Specimen Data
Your Task
Create a macro that formats raw specimen data professionally.
Target Sheet: Exercise1_Raw in Tensile_Test_Sample.xlsx
Requirements:
- Add headers:
- A1: "Specimen No"
- B1: "Width (mm)"
- C1: "Thickness (mm)"
- D1: "CSA (mm²)"
-
E1: "Young's Modulus (GPa)"
-
Format header row:
- Bold text
- Centre alignment
-
Light blue background
RGB(173, 216, 230) -
Format data:
- 3 decimal places for columns B:E
- Borders around entire table
- Auto-fit columns
Macro name: FormatSpecimenData
Shortcut: Ctrl+Shift+F
Step-by-Step Instructions
1. Download and Open the File
- Download Tensile_Test_Sample.xlsx
- Open it in Excel
- Navigate to the
Exercise1_Rawsheet - You should see raw data without formatting
2. Record the Macro
Start Recording:
- Developer → Record Macro
- Name:
FormatSpecimenData - Shortcut:
Ctrl+Shift+F - Store in: This Workbook
- Click OK (recording starts!)
Perform These Actions:
Record Carefully
Excel is recording every action. Work methodically!
Headers (add units):
- Click A1, type:
Specimen No, press Enter - Click B1, type:
Width (mm), press Enter - Click C1, type:
Thickness (mm), press Enter - Click D1, type:
CSA (mm²), press Enter - Click E1, type:
Young's Modulus (GPa), press Enter
Format Headers:
- Select A1:E1 (click A1, drag to E1)
- Click Bold button (or Ctrl+B)
- Click Center align button
- Click Fill Color → Choose light blue
Format Data:
- Select B2:E7 (or select B2, press Ctrl+Shift+End to select to last cell)
- Right-click → Format Cells
- Category: Number
- Decimal places: 3
- Click OK
Add Borders:
- Select A1:E7 (entire table)
- Home → Borders → All Borders
Auto-fit:
- Select columns A:E (click column A header, drag to E)
- Double-click any column divider (between letters at top)
Stop Recording:
- Developer → Stop Recording
3. Test Your Macro
Save First!
File → Save As → Save as .xlsm format!
Test it:
- Press
Ctrl+Zrepeatedly to undo formatting (get back to raw data) - Press
Ctrl+Shift+F(your macro shortcut) - Watch Excel format everything automatically!
Bonus Challenge: Edit to Improve
Ready to level up? Let's improve the code!
Bonus Challenge
Task: Edit the macro to make it more robust
Steps:
- Press Alt+F11 (open VBA Editor)
- Find your
FormatSpecimenDatamacro - Make these improvements:
Remove .Select statements:
Change this:
To this:
Make row count dynamic:
Add at the top (after Dim if you have one, or as first line):
Change fixed ranges like B2:E7 to dynamic: B2:E & lastRow`
Add user feedback:
Add at the end (before End Sub):
Save and test!
Solution
Need Help?
If you're stuck or want to compare your solution:
- Full solution with explanation: Exercise_Solutions.md
- Working code file: Exercise_Solutions.bas
But try it yourself first! Learning happens when you struggle a bit.
What You've Practiced
✓ Recording a complete macro from scratch
✓ Working with headers and formatting
✓ Using colour and alignment properties
✓ Testing macros safely
✓ (Bonus) Editing VBA to improve recorded code
✓ (Bonus) Making ranges dynamic
Common Issues
My macro only formats 7 rows, but I have more data!
Your recorded range was hard-coded (e.g., A1:E7).
Fix: Edit the macro to use dynamic range:
Ctrl+Z doesn't undo my macro!
This is normal! Macros clear Excel's undo history.
Solution: Always save before running macros. If you need to undo, close without saving and reopen the file.
I get an error when running the macro
Check:
- Are you on the correct sheet (
Exercise1_Raw)? - Did you save as
.xlsmformat? - Is there actually data in the sheet?
Press Debug when error appears to see which line failed.
Next: Creating Buttons
Typing keyboard shortcuts is good, but clickable buttons are even better! Let's make a professional interface.
Continue to Creating Buttons →