12.5 Sub Procedures
A macro is a Sub procedure: it starts with Sub Name() and ends with End Sub. Comments start with an apostrophe '.
Option Explicit
Sub HelloBlinkit()
' My first macro
Worksheets("Report").Range("A1").Value = "FreshGo Maharashtra - Daily Report"
Worksheets("Report").Range("A2").Value = Date
Worksheets("Report").Range("A2").NumberFormat = "dd-mm-yyyy"
MsgBox "Report header ready!", vbInformation, "Done"
End Sub
- Run: put the cursor inside the Sub and press F5.
- One Sub can call another:
Call FormatHeaderor justFormatHeader. Private Subhides a macro from the Macros dialog (useful for helper procedures).- Line continuation: end a line with a space and underscore
_to continue on the next line.
Ravindra Bagale's Tip
After running a macro, Undo (Ctrl + Z) doesn't work – many students run a wrong macro once and the data is ruined. Always test a new macro on a copy of the file (a backup). And give the Sub a meaningful name (FormatDailyReport), not Macro1.
Ravindra Bagale's Tip – मराठी
Macro run केल्यावर Undo (Ctrl + Z) चालत नाही – बरेच students एकदा चुकीचा macro चालवतात आणि data खराब होतो. नवीन macro नेहमी file ची copy (backup) करून test करा. आणि Sub चं नाव अर्थपूर्ण ठेवा (FormatDailyReport), Macro1 नको.
Ravindra Bagale's Tip – हिंदी
Macro run करने के बाद Undo (Ctrl + Z) नहीं चलता – बहुत से students एक बार गलत macro चला देते हैं और data ख़राब हो जाता है. नया macro हमेशा file की copy (backup) पर test करो. और Sub का नाम मतलब वाला रखो (FormatDailyReport), Macro1 नहीं.
Worked example — sub procedures
Why
Sana Sheikh in Pune and James O'Neill abroad both need a clear, repeatable way to use sub procedures on a HomeNest Supplies sheet without breaking totals.
What
Sub Procedures is the Excel idea this page teaches. We will walk one tiny example end to end.
How
- Open a blank sheet. In A1 type a short label for sub procedures.
- Enter a small made-up value for Sana Sheikh using € where money appears.
- Add a second value for James O'Neill using £ so you see mixed currencies stay numeric.
- Apply the method from this lesson step by step (menu or formula as shown above).
- Check the result still works inside SUM or a simple check cell.
Formula / action
Follow the formula or ribbon steps shown in this lesson (keep values numeric).
Meaning: Once sub procedures is correct on one row, copy the same pattern down the column.
Practice tasks (10+)
Try these on a blank sheet. Open a hint only if you are stuck. No quiz — just practice.
1. Rebuild the smallest example of sub procedures on a blank sheet for Pearl Textiles.
Hint: Use only the steps on this page. Keep numbers numeric.
2. Change one input for Priya Deshmukh and confirm the result updates.
Hint: If the result is stuck, check absolute vs relative references.
3. Repeat the same steps with a £ or € value for Liam Murphy.
Hint: Formatting must not break SUM.
4. Write the result meaning in one plain sentence next to the cell.
Hint: Imagine explaining it to a new joiners.
5. Add a second row and fill the method down.
Hint: Double-click the fill handle if the pattern is consistent.
6. Break the example on purpose, then fix it using this lesson.
Hint: Common break: text instead of a number.
7. Apply sub procedures to a 5-row made-up Pearl Textiles table.
Hint: Keep column headers clear.
8. Check your answer with a simple total or COUNT.
Hint: A wrong total usually means a format or key mismatch.
9. Swap ₹ and $ on one column and re-check totals.
Hint: Stored values stay the same; only the display changes.
10. List two mistakes beginners make with sub procedures.
Hint: Look back at the tip box on this page.
11. Teach the steps out loud in under one minute.
Hint: Why → What → How.
12. Save the sheet as PearlTextil_practice.xlsx.
Hint: Use a clear file name for class.
Loading practice tasks...
- Last used row (Easy): Which row number is the last filled row? The headings are in row 1.
- Late orders (Easy): How many rows have Status "Late"?
- Total amount (Easy): Add up the Amount column.
- Worst delay (Easy): What is the largest number in the Days late column?
- Late orders in Pune (Medium): How many orders are Late and in City "Pune"?
- Customer for an order (Medium): Find the Customer for Order ID "DL-704".
- Money stuck in late orders (Medium): Add up the Amount column for rows where Status is "Late".
- Big orders (Medium): How many rows have an Amount of 2000 or more?
- Message text (Medium): Build the text "N late orders", where N is the count of Late rows.
- Average delay of late orders (Hard): What is the average Days late for rows with Status "Late"? Round to 1 decimal.
- Tag for the first row (Hard): For the first row, return "Chase" if Days late is 3 or more, else "OK".
- Warning or all clear (Hard): Return "Warning" if any order is 3 or more days late, else "All clear".
Quick revision cards
Three standalone cards. Each one explains the idea with its own example. Scan the QR to open this lesson.


