0% found this document useful (0 votes)
28 views

Create Your Own Functions in Excel VBA

Functions in Excel VBA return a value, unlike subs. When creating a function, you declare the function name followed by parentheses and specify the return type using "As" just like a variable. Inside the function, you assign a value to the function name which is returned.

Uploaded by

prakash
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Create Your Own Functions in Excel VBA

Functions in Excel VBA return a value, unlike subs. When creating a function, you declare the function name followed by parentheses and specify the return type using "As" just like a variable. Inside the function, you assign a value to the function name which is returned.

Uploaded by

prakash
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Create your own Functions in Excel VBA

Functions are closely related to the Subs you learned about in a previous section, and are set up
in a similar way. The difference is that functions return a value (like the MsgBox function)
whereas Subs don't return a value - they just get on and execute the code. You use a function
when you want a chunk of code to return some sort of answer for you.

When you set up a function, you set it up like this:

Function function_name( ) As variable_type

You start with the word Function. After a space, you need to come up with a name for your
function. Like Sub names, this should be something relevant to what the function does.

After the name of your function, you need a pair of round brackets. Just like Subs, the round
brackets are used for any arguments you want to pass over to your function.

One of the big differences between setting up a Sub and setting up a Function is the return type at
the end. This is exactly the same as setting up a variable type. So you can have As String, or As
Boolean, or As Integer - any of the types you can use with ordinary variables can also be used
with functions. If you miss off the As Type at the end then the function will be As Variant.

The function name you come up with is like a variable: whatever answer you want your function
to produce will be stored in the function name.

To return a value from your functions you need this:

function_name = value

You might also like