Excel like a finance pro.
VBA · Syntax
Function procedures
A block of code that returns a value, usable from other code or as a custom worksheet function.
When to use it
A Function procedure returns a value. Call it from other code, or from a worksheet cell as a custom function when it lives in a standard module.
The code
- Code
Function Double(x As Double) As Double Double = x * 2 End Function
Worked examples
Return a value
Function Double(x As Double) As Double Double = x * 2 End Function → Assign to the function name to return
=Double(A1) works in a cell.
A Boolean helper
Function IsWeekend(d As Date) As Boolean IsWeekend = Weekday(d, vbMonday) > 5 End Function → TRUE for Saturday and Sunday
A tidy test reused everywhere.
Wrap a pattern
Function LastRow(ws As Worksheet) As Long LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row End Function → The last used row of any sheet
Wrap common patterns.
Worth knowing
- Declare the return type after the closing parenthesis.
- Functions used in cells must not change other cells or formatting.
- Exit Function leaves early with the current return value.
Where it goes wrong
- Forgetting to assign the function name returns 0 or an empty string.
- A cell calling a UDF shows #NAME? until macros are enabled.
Related
Learn the moves here — or let Wauvel run them on your numbers.
Meet your AI CFO →One CFO-grade Excel tip a week
A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.