Ravindra BagaleCourses & study guides मराठी Track your progress

12. Macros and VBA

12.13 User-Defined Functions: a Delivery Fee Function

User-defined function (DeliveryFee) with example — in 30 seconds

What: A user-defined function is your own worksheet function written in VBA. You use it like SUM or XLOOKUP.

Why: The fee rules live in one place. Every sheet that calls DeliveryFee gets the same answer.

Tiny example: =DeliveryFee(270) gives 15. On a festival evening =DeliveryFee(270, TRUE) adds ₹10 and gives 25.

First look at the data (before → after):

User-defined function (DeliveryFee) with example: before and after tables, fictional FreshGo Maharashtra data

Expected result — Calls in the worksheet

Formula Result (₹)
=DeliveryFee(64) 30
=DeliveryFee(150) 25
=DeliveryFee(270) 15
=DeliveryFee(270, TRUE) 25
=DeliveryFee(1299, TRUE) 0

About this data: Teaching sample for a fictional shop “QuickCart Delivery”. Figures and names (e.g. Aarti Kulkarni) are made up for this lesson on user defined functions a delivery fee function.

Before table and formula as text

Before — Fee rules (fictional)

Order amount Fee (₹)
below 99 30
99 to 198 25
199 to 498 15
499 and above 0
Festival +10 on paid fees

About this data: Demo table only — not real customer or company data. Built to practise user defined functions a delivery fee function.

Formula / click path

Public Function DeliveryFee(ByVal orderAmount As Double, Optional ByVal isFestival As Boolean = False) As Double
    Select Case … (slabs) … If isFestival And fee > 0 Then fee = fee + 10

Do it step by step:

  1. Press Alt + F11 and click Insert › Module.
  2. Paste the DeliveryFee function from this section.
  3. Save the workbook as Excel Macro-Enabled Workbook (*.xlsm).
  4. In a cell, type =DeliveryFee(270) and press Enter.
  5. Try =DeliveryFee(270, TRUE) and =DeliveryFee(1299, TRUE). Check 25 and 0.

A Function returns a value and can be used in worksheet formulas like a built-in function. It must be in a standard module.

Public Function DeliveryFee(ByVal orderAmount As Double, _
                            Optional ByVal isFestival As Boolean = False) As Double
    ' Fictional slabs: <99 = 30, 99-198 = 25, 199-498 = 15, 499+ = free
    Dim fee As Double
    Select Case orderAmount
        Case Is >= 499: fee = 0
        Case Is >= 199: fee = 15
        Case Is >= 99: fee = 25
        Case Else: fee = 30
    End Select
    ' Festival surcharge of Rs 10 on paid deliveries (e.g. Diwali evenings)
    If isFestival And fee > 0 Then fee = fee + 10
    DeliveryFee = fee
End Function
Formula Result
=DeliveryFee(64) 30
=DeliveryFee(270) 15
=DeliveryFee(270, TRUE) 25
=DeliveryFee(1299, TRUE) 0

About this data: Made-up rows so you can copy the pattern for user defined functions a delivery fee function. Currencies may use ₹, £, € or $.

It appears in Formulas › Insert Function under User Defined. The workbook must be .xlsm; to use it in every workbook, save it in an add-in (.xlam) or the Personal Macro Workbook (then call =PERSONAL.XLSB!DeliveryFee(G2)).

Ravindra Bagale's Tip

Many students try to change other cells' formats or values from a UDF – a function called from the worksheet can't do that; it can only return a value. Assign the value to the function's own name inside the function (DeliveryFee = fee), otherwise the result is 0. And a UDF can be slower than worksheet functions – use it carefully on lakhs of rows.

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 user defined functions a delivery fee function on a blank sheet for NovaCraft Tools.

Hint: Use only the steps on this page. Keep numbers numeric.

2. Change one input for Aamir Khan 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 user defined functions a delivery fee function to a 5-row made-up NovaCraft Tools 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 user defined functions a delivery fee function.

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 NovaCraftTo_practice.xlsx.

Hint: Use a clear file name for class.

Loading practice tasks...

  1. Total order value (Easy): Add up Order value (£) in C2:C11.
  2. Total fees (Easy): Add up Fee (£) in E2:E11.
  3. Due rows (Easy): How many rows have Status "Due"?
  4. Farthest order (Easy): Largest Distance km in D2:D11.
  5. Pune rows (Easy): How many rows have City "Pune"?
  6. Due fees total (Medium): Total Fee (£) where Status is "Due".
  7. Distance over 8 (Medium): How many orders have Distance km > 8?
  8. Fee of O-101 (Medium): Fee (£) for Order "O-101".
  9. London value (Medium): Total Order value (£) where City is "London".
  10. Share Due fees (Hard): Due fees / all fees, 3 decimals.
  11. Avg fee (Hard): Average Fee (£), 2 decimals.
  12. Far flag (Hard): "Far" if D2 > 8 else "Near".

Quick revision cards

Three standalone cards. Each one explains the idea with its own example. Scan the QR to open this lesson.

Revision card 1: What is User Defined Functions A Delivery Fee Function?
Card 1: What is User Defined Functions A Delivery Fee Function? · Download PNG
Revision card 2: Tiny example — User Defined Functions A Delivery Fee Function
Card 2: Tiny example — User Defined Functions A Delivery Fee Function · Download PNG
Revision card 3: Checklist — User Defined Functions A Delivery Fee Function
Card 3: Checklist — User Defined Functions A Delivery Fee Function · Download PNG