# 12.13 UDF — worksheetमध्ये वापरायचं स्वतःचं function

Source: https://ravindrabagale.com/mr/excel/ch12-macros-and-vba/12-13-user-defined-functions-a-delivery-fee-function.html
Language: mr (Marathi with English technical terms)

Function value परत करतो. Standard moduleमध्ये Public Function लिहिलं की worksheet formulaसारखं वापरता येतं.

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

DeliveryFeeच्या rates आणि festival surcharge काल्पनिक आहेत. ≥499 free; ≥199 ₹15; ≥99 ₹25; त्याखाली ₹30. Paid deliveryवर festival TRUE असल्यास ₹10 वाढतो.

 | Formula
 | Result

 | =DeliveryFee(64)
 | 30

 | =DeliveryFee(270)
 | 15

 | =DeliveryFee(270, TRUE)
 | 25

 | =DeliveryFee(1299, TRUE)
 | 0

Insert Functionमध्ये User Defined category पाहा. Workbook .xlsmमध्ये जपा. सर्व workbooksमध्ये वापरण्यासाठी controlled .xlam add-in किंवा PERSONAL.XLSBमधून =PERSONAL.XLSB!DeliveryFee(G2) reference देता येतो; ती code file उपलब्ध/open हवी.

Worksheetमधून call केलेल्या UDFने इतर cells बदलणे किंवा formatting करणे अपेक्षित नाही; ते Subने करा. Return value functionच्या नावाला assign करा: DeliveryFee = fee. Source function negative किंवा invalid amountsसाठी स्वतंत्र policy देत नाही; production input validation जोडा.

Practice

IsLate(mins, Optional limit=15) Boolean आणि FinancialYear(d) “FY 2026-27” label देणारं UDF लिहा. FY Aprilपासून सुरू होतो अशी assumption स्पष्ट करा. 31 March/1 April आणि 15/16 minutes boundary tests करा.

रवींद्र बागले यांची tip

मोठ्या dataवर UDF हजारोदा call होऊ शकतो. आधी built-in formula किंवा LAMBDAने काम होतं का पाहा; speed आणि compatibility तपासा.
