VbaFunctionsBeginner

Function procedure

Defines reusable logic that returns a value.

Review the syntaxStudy the examplesOpen the coding app
Function Name(arg1 As Double) As Double
    Name = arg1 * 2
End Function

This 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

arg1 (optional)Variant · Input value or object passed into the function.

See it in practice

Examples

1

Return a cleaned invoice code

Function NormaliseCode(rawValue As String) As String
    NormaliseCode = Trim$(UCase$(rawValue))
End Function

A Function keeps reusable transformation logic out of the calling macro.

Debug faster

Common Errors

1

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

Excel desktop VBA

Worksheet UDFs cannot freely change workbook state the way macros do.

Source: Microsoft Learn Office VBA reference

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.