🎉 Black Friday: 40% OFF every course with code BF40 · ends 1 Dec  See courses →
A new chapter in practical learning · Now enrolling: Advanced Excel & Power Query, Excel + AI Mastery, Excel Essentials in 2 Hours, Excel Pivot Tables & Dashboards, Excel VBA Advanced with AI, Excel VBA Basics with AI, Financial Modeling in Excel, Google Sheets Complete Course, Power BI and Project Management in ExcelView the courses ↗
← Course overviewExcel VBA Advanced with AI
Free preview lessonExcel VBA Advanced with AI84 lessons · 6h 28m · Certificate of completion
Regular price ₹4,999.00 ₹2,999.00
View course details
MODULE 2 / 14LESSON 3 OF 84

Sub vs Function and passing arguments

Module 1: Procedures, Functions and Custom UDFs · 8 min

In this lesson

Break one long macro into small Subs and Functions that take arguments and return values, with one short main Sub that calls them.

The business scenario

Copper Creek's monthly report macro is one long Sub of 57 lines: it finds the last row twice, formats two headers with copied code and loops the 5,252 October 2026 order lines three times. We split it into LastRow(ws), FormatHeader(rng), NetTotal(ws), CountStatus(ws, status) and a 9-line main Sub that gives exactly the same results.

VBA in this lesson: Sub, Function ... As Long, arguments, Call, return values, Private vs Public, Optional (preview)

In this video

  • 00:00 Welcome
  • 00:22 Paste the long macro
  • 00:47 Run the long macro
  • 01:31 Spot the copied blocks
  • 01:56 Pause and predict
  • 02:15 Sub or Function?
  • 02:31 The main Sub first
  • 03:08 Function LastRow
  • 03:44 Sub FormatHeader
  • 04:12 Common mistake: brackets
  • 04:28 NetTotal and CountStatus
  • 05:03 WriteResult and compile
  • 05:37 Run the short main Sub
  • 06:18 Private helpers stay hidden
  • 06:38 Your Turn and next lesson

Check your results

After you run the macro(s) you should see:

  • Summary C4 - order lines (long macro): 5,252
  • Summary C5 - net amount (long macro): 6,582,833.74
  • Summary D4 - order lines (refactored): 5,252
  • Summary D5 - net amount (refactored): 6,582,833.74
  • Summary D6 - Delivered lines: 3,069
  • Summary D7 - Returned lines: 296
  • Message box says "Order lines: 5252": TRUE
  • Orders A1 is bold: TRUE
  • Orders N1 fill is RGB(31, 58, 95): 6,240,799
  • Summary D8 - Pending lines (Your Turn 2): 331
  • Summary D9 - total Qty (Your Turn 1): 67,923

Common mistakes

  • Forgetting to assign the function name: a Function that never says LastRow = ... returns 0.
  • Brackets without Call on a Sub with two arguments: FormatHeader(a, b) is a syntax error - write FormatHeader a, b or Call FormatHeader(a, b).
  • Making a helper Private and then trying to run it from Alt+F8 - only Public procedures without arguments show there.

Your turn

  1. Add Private Function SumColumn(ws As Worksheet, col As Long) As Double and a macro ShowTotalQty that writes the total Qty (column J = 10) into Summary D9 and shows it in a message box. (Hint: Loop For r = 2 To LastRow(ws))
  2. Write ShowPending: write the number of Pending lines into Summary D8 with CountStatus. (Hint: WriteResult 8, CountStatus(...))

Solutions are at the bottom of Lesson_1_1_Code.bas.

Practice files

Download Module-01-Functions-and-UDFs-Practice-Files.zip from the Practice resources tab and open the folder 1.1 Sub vs Function and passing arguments: Lesson_1_1_Notes.pdf, Lesson_1_1_Practice.xlsm, Lesson_1_1_Start_MonthlyReportLong.txt. The finished workbooks and code are in Module-01-Functions-and-UDFs-Solutions.zip - open them only after your own attempt.

Copper Creek Home Goods and every other business in this course are fictional; all names and numbers are for training only.

FREE EXCEL LESSONS BY EMAIL

Not ready to enroll yet?

Join the Academy email list for practical Excel and AI tips, free preview lessons and news when a new course opens. Unsubscribe at any time.