Function procedure
Defines reusable logic that returns a value.
Function Name(arg1 As Double) As Double
Name = arg1 * 2
End FunctionThis static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Defines reusable logic that returns a value.
Use a Function when you need a result back from VBA, whether that is a computed number, a cleaned string, or a Boolean check. In Excel automation this often appears in helper code behind larger macros and sometimes as a worksheet UDF.
Quick reference
Syntax
Function Name(arg1 As Double) As Double
Name = arg1 * 2
End Function
Inputs
Parameters
See it in practice
Examples
Return a cleaned invoice code
Function NormaliseCode(rawValue As String) As String
NormaliseCode = Trim$(UCase$(rawValue))
End FunctionA Function keeps reusable transformation logic out of the calling macro.
Debug faster
Common Errors
LogicError
Cause: Calculating a result but never assigning it to the function name.
Fix: Assign the return value to the function name before End Function.
Runtime support
Compatibility
Worksheet UDFs cannot freely change workbook state the way macros do.
Common questions
Frequently Asked Questions
Defines reusable logic that returns a value.
arg1: Input value or object passed into the function.
LogicError: Assign the return value to the function name before End Function.