Just setting the return value to the function name is still not exactly the same as the Java (or other) return
statement, because in java, return
exits the function, like this:
public int test(int x) {
if (x == 1) {
return 1; // exits immediately
}
// still here? return 0 as default.
return 0;
}
In VB, the exact equivalent takes two lines if you are not setting the return value at the end of your function. So, in VB the exact corollary would look like this:
Public Function test(ByVal x As Integer) As Integer
If x = 1 Then
test = 1 ' does not exit immediately. You must manually terminate...
Exit Function ' to exit
End If
' Still here? return 0 as default.
test = 0
' no need for an Exit Function because we're about to exit anyway.
End Function
Since this is the case, it’s also nice to know that you can use the return variable like any other variable in the method. Like this:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test <> 1 Then ' Test the currently set return value
test = 0 ' Reset the return value to a *new* value
End If
End Function
Or, the extreme example of how the return variable works (but not necessarily a good example of how you should actually code)—the one that will keep you up at night:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test > 0 Then
' RECURSIVE CALL...WITH THE RETURN VALUE AS AN ARGUMENT,
' AND THE RESULT RESETTING THE RETURN VALUE.
test = test(test - 1)
End If
End Function
In this Article
- Creating a Function without Arguments
- Calling a Function from a Sub Procedure
- Creating Functions
- Single Argument
- Multiple Arguments
- Optional Arguments
- Default Argument Value
- ByVal and ByRef
- Exit Function
- Using a Function from within an Excel Sheet
This tutorial will teach you to create and use functions with and without parameters in VBA
VBA contains a large amount of built-in functions for you to use, but you are also able to write your own. When you write code in VBA, you can write it in a Sub Procedure, or a Function Procedure. A Function Procedure is able to return a value to your code. This is extremely useful if you want VBA to perform a task to return a result. VBA functions can also be called from inside Excel, just like Excel’s built-in Excel functions.
Creating a Function without Arguments
To create a function you need to define the function by giving the function a name. The function can then be defined as a data type indicating the type of data you want the function to return.
You may want to create a function that returns a static value each time it is called – a bit like a constant.
Function GetValue() As Integer
GetValue = 50
End Function
If you were to run the function, the function would always return the value of 50.
You can also create functions that refer to objects in VBA but you need to use the Set Keyword to return the value from the function.
Function GetRange() as Range
Set GetRange = Range("A1:G4")
End Function
If you were to use the above function in your VBA code, the function would always return the range of cells A1 to G4 in whichever sheet you are working in.
Calling a Function from a Sub Procedure
Once you create a function, you can call it from anywhere else in your code by using a Sub Procedure to call the function.
The value of 50 would always be returned.
You can also call the GetRange function from a Sub Procedure.
In the above example, the GetRange Function is called by the Sub Procedure to bold the cells in the range object.
Creating Functions
Single Argument
You can also assign a parameter or parameters to your function. These parameters can be referred to as Arguments.
Function ConvertKilosToPounds (dblKilo as Double) as Double
ConvertKiloToPounds = dblKilo*2.2
End Function
We can then call the above function from a Sub Procedure in order to work out how many pounds a specific amount of kilos are.
A function can be a called from multiple procedures within your VBA code if required. This is very useful in that it stops you from having to write the same code over and over again. It also enables you to divide long procedures into small manageable functions.
In the above example, we have 2 procedures – each of them are using the Function to calculate the pound value of the kilos passed to them in the dblKilo Argument of the function.
Multiple Arguments
You can create a Function with multiple arguments and pass the values to the Function by way of a Sub Procedure.
Function CalculateDayDiff(Date1 as Date, Date2 as Date) as Double
CalculateDayDiff = Date2-Date1
End Function
We can then call the function to calculate the amount of days between 2 dates.
Optional Arguments
You can also pass Optional arguments to a Function. In other words, sometimes you may need the argument, and sometimes you may not – depending on what code you are using the Function with .
Function CalculateDayDiff(Date1 as Date, Optional Date2 as Date) as Double
'check for second date and if not there, make Date2 equal to today's date.
If Date2=0 then Date2 = Date
'calculate difference
CalculateDayDiff = Date2-Date1
End Function
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More
Default Argument Value
You can also set the default value of the Optional arguments when you are creating the function so that if the user omits the argument, the value that you have put as default will be used instead.
Function CalculateDayDiff(Date1 as Date, Optional Date2 as Date="06/02/2020") as Double
'calculate difference
CalculateDayDiff = Date2-Date1
End Function
ByVal and ByRef
When you pass values to a function, you can use the ByVal or ByRef keywords. If you omit either of these, the ByRef is used as the default.
ByVal means that you are passing a copy of the variable to the function, whereas ByRef means you are referring to the original value of the variable. When you pass a copy of the variable (ByVal), the original value of the variable is NOT changed, but when you reference the variable, the original value of the variable is changed by the function.
Function GetValue(ByRef intA As Integer) As Integer
intA = intA * 4
GetValue = intA
End Function
In the function above, the ByRef could be omitted and the function would work the same way.
Function GetValue(intA As Integer) As Integer
intA = intA * 4
GetValue = intA
End Function
To call this function, we can run a sub-procedure.
Sub TestValues()
Dim intVal As Integer
'populate the variable with the value 10
intVal = 10
'run the GetValue function, and show the value in the immediate window
Debug.Print GetValue(intVal)
'show the value of the intVal variable in the immediate window
Debug.Print intVal
End Sub
Note that the debug windows show the value 40 both times. When you pass the variable IntVal to the function – the value of 10 is passed to the function, and multiplied by 4. Using the ByRef keyword (or omitting it altogether), will AMEND the value of the IntVal variable. This is shown when you show first the result of the function in the immediate window (40), and then the value of the IntVal variable in the debug window (also 40).
If we do NOT want to change the value of the original variable, we have to use ByVal in the function.
Function GetValue(ByVal intA As Integer) As Integer
intA = intA * 4
GetValue = intA
End Function
Now if we call the function from a sub-procedure, the value of the variable IntVal will remain at 10.
Exit Function
If you create a function that tests for a certain condition, and once the condition is found to be true, you want return the value from the function, you may need to add an Exit Function statement in your Function in order to exit the function before you have run through all the code in that function.
Function FindNumber(strSearch As String) As Integer
Dim i As Integer
'loop through each letter in the string
For i = 1 To Len(strSearch)
'if the letter is numeric, return the value to the function
If IsNumeric(Mid(strSearch, i, 1)) Then
FindNumber= Mid(strSearch, i, 1)
'then exit the function
Exit Function
End If
Next
FindNumber= 0
End Function
The function above will loop through the string that is provided until it finds a number, and then return that number from the string. It will only find the first number in the string as it will then Exit the function.
The function above can be called by a Sub routine such as the one below.
Sub CheckForNumber()
Dim NumIs as Integer
'pass a text string to the find number function
NumIs = FindNumber("Upper Floor, 8 Oak Lane, Texas")
'show the result in the immediate window
Debug.Print NumIs
End Sub
VBA Programming | Code Generator does work for you!
Using a Function from within an Excel Sheet
In addition to calling a function from your VBA code using a sub procedure, you can also call the function from within your Excel sheet. The functions that you have created should by default appear in your function list in the User Defined section of the function list.
Click on the fx to show the Insert Function dialog box.
Select User Defined from the Category List
Select the function you require from the available User Defined Functions (UDF’s).
Alternatively, when you start writing your function in Excel, the function should appear in the drop down list of functions.
If you do not want the function to be available inside an Excel sheet, you need to put the Private word in front of the word Function when you create the function in your VBA code.
Private Function CalculateDayDiff(Date1 as Date, Date2 as Date) as Double
CalculateDayDiff = Date2-Date1
End Function
It will now not appear in the drop down list showing the Excel functions available.
Interestingly enough, however, you can still use the function – it just will not appear in the list when looking for it!
If you have declared the second argument as Optional, you can omit it within the Excel sheet as well as within the VBA code.
You can also use the a function that you have created without arguments in your Excel sheet.
As You may know, in general in programming to return a result from function there is Return statement. Even Visual Basic has it. Unfortunately there is no such thing in VBA. In this article I’m going to show You how to return a result from VBA function.
Let’s start with function
For the purpose of this article I’ve created functionModulo, which is very similar to the Mod function.
Function functionModulo(x As Integer, y As Integer) As Boolean
Dim divisionRest As Double, divisionRestStatus As Integer
Let divisionRest = x / y - Fix(x / y)
If divisionRest > 0 Then
divisionRestStatus = 0
Else
divisionRestStatus = 1
End If
If divisionRestStatus = 1 Then
functionModulo = True
Else
functionModulo = False
End If
End Function
This function allows You to check if there is a divisibility between two numbers. If there is a rest from division the function returns False, if there is no any – function returns True.
So to describe the work of this function, I was using return word. But not as a statement in code. To return the result in VBA function You need to use the function name itself and put it a value.
functionModulo = True
Unfortunately the function not stops there and exits. It just go further.
As close as possible
I decided to modify this function to make it as similar to Return statement schema as possible.
So first of all I cut it a little bit.
Function functionModulo(x As Integer, y As Integer) As Boolean
Dim divisionRest As Double
Let divisionRest = x / y - Fix(x / y)
If divisionRest > 0 Then
functionModulo = False
Else
functionModulo = True
End If
End Function
Then I wanted to have the same effect after returning the result from function as Return statement brings.
Function functionModulo(x As Integer, y As Integer) As Boolean
Dim divisionRest As Double
Let divisionRest = x / y - Fix(x / y)
If divisionRest > 0 Then functionModulo = False: Exit Function
If divisionRest = 0 Then functionModulo = True
End Function
That’s why after returning the value I added Exit Function.
If divisionRest > 0 Then functionModulo = False: Exit Function
I was almost done, but also figured out that we can only check for valid values. This means that I could set False value at the beginning as default. One conditional less in code. Cleaner the code.
Function functionModulo(x As Integer, y As Integer) As Boolean
functionModulo = False
Dim divisionRest As Double: Let divisionRest = x / y - Fix(x / y)
If divisionRest = 0 Then functionModulo = True
End Function
Having nothing after the conditional for True value I decided to erase Exit Function statement. In this case it is not needed, but in general it is really good approach to simulate Return statement as from other programming languages.
To sum up
To avoid unnecessary conditionals put the default value at the beginning of the function. You can also do that in the end of the code, only if You simulate the Return statement in VBA – pass the result in the function name and Exit Function just right after that line.
I’m very advanced in VBA, Excel, also easily linking VBA with other Office applications (e.g. PowerPoint) and external applications (e.g. SAP). I take part also in RPA processes (WebQuery, DataCache, IBM Access Client Solutions) where I can also use my SQL basic skillset. I’m trying now to widen my knowledge into TypeScript/JavaScript direction.
View all posts by Tomasz Płociński
“Computers are useless. They can only give you answers.” – Pablo Picasso.
This post provides a complete guide to using the VBA Sub. I also cover VBA functions which are very similar to Subs.
If you want some quick information about creating a VBA Sub, Function, passing parameters, return values etc. then check out the quick guide below.
If you want to understand all about the VBA Sub, you can read through the post from start to finish or you can check out the table of contents below.
Quick Guide to the VBA Sub
Sub | Example |
---|---|
Sub
|
|
Function
|
|
Create a sub | Sub CreateReport()
End Sub |
Create a function | Function GetTotal() As Long
End Function |
Create a sub with parameters | Sub CreateReport(ByVal Price As Double)
Sub CreateReport(ByVal Name As String) |
Create a function with parameters | Function GetTotal(Price As Double)
Function GetTotal(Name As String) |
Call a sub | Call CreateReport ‘ Or CreateReport |
Call a function | Call CalcPrice ‘ Or CalcPrice |
Call a sub with parameters | Call CreateReport(12.99)
CreateReport 12.99 |
Call a function with parameters | Call CalcPrice(12.99)
CalcPrice 12.99 |
Call a function and retrieve value (cannot use Call keyword for this) |
Price = CalcPrice |
Call a function and retrieve object | Set coll = GetCollection |
Call a function with params and retrieve value/object | Price = CalcPrice(12) Set coll = GetCollection(«Apples») |
Return a value from Function | Function GetTotal() As Long GetTotal = 67 End Function |
Return an object from a function | Function GetCollection() As Collection Dim coll As New Collection Set GetCollection = coll End Function |
Exit a sub | If IsError(Range(«A1»)) Then Exit Sub End If |
Exit a function | If IsError(Range(«A1»)) Then Exit Function End If |
Private Sub/Private Function (available to current module) |
Private Sub CreateReport() |
Public Sub/Public Function (available to entire project) |
Public Sub CreateReport() |
Introduction
The VBA Sub is an essential component of the VBA language. You can also create functions which are very similar to subs. They are both procedures where you write your code. However, there are differences and these are important to understand. In this post I am going to look at subs and functions in detail and answer the vital questions including:
- What is the difference between them?
- When do I use a sub and when do I use a function?
- How do you run them?
- Can I return values?
- How do I pass parameters to them?
- What are optional parameters?
- and much more
Let’s start by looking at what is the VBA sub?
What is a Sub?
In Excel VBA a sub and a macro are essentially the same thing. This often leads to confusion so it is a good idea to remember it. For the rest of this post I will refer to them as subs.
A sub/macro is where you place your lines of VBA code. When you run a sub, all the lines of code it contains are executed. That means that VBA carries out their instructions.
The following is an example of an empty sub
Sub WriteValues() End Sub
You declare the sub by using the Sub keyword and the name you wish to give the sub. When you give it a name keep the following in mind:
- The name must begin with a letter and cannot contain spaces.
- The name must be unique in the current workbook.
- The name cannot be a reserved word in VBA.
The end of the Sub is marked by the line End Sub.
When you create your Sub you can add some code like the following example shows:
Sub WriteValues() Range("A1") = 6 End Sub
What is a Function?
A Function is very similar to a sub. The major difference is that a function can return a value – a sub cannot. There are other differences which we will look at but this is the main one.
You normally create a function when you want to return a value.
Creating a function is similar to creating a sub
Function PerformCalc() End Function
It is optional to add a return type to a function. If you don’t add the type then it is a Variant type by default. This means that VBA will decide the type at runtime.
The next example shows you how to specify the return type
Function PerformCalc() As Long End Function
You can see it is simple how you declare a variable. You can return any type you can declare as a variable including objects and collections.
A Quick Comparison
Sub:
- Cannot return a value.
- Can be called from VBAButtonEvent etc.
Function
- Can return a value but doesn’t have to.
- Can be called it from VBAButtonEvent etc. but it won’t appear in the list of Macros. You must type it in.
- If the function is public, will appear in the worksheet function list for the current workbook.
Note 1: You can use “Option Private Module” to hide subs in the current module. This means that subs won’t be visible to other projects and applications. They also won’t appear in a list of subs when you bring up the Macro window on the developer tab.
Note 2:We can use the word procedure to refer to a function or sub
Calling a Sub or Function
When people are new to VBA they tend to put all the code in one sub. This is not a good way to write your code.
It is better to break your code into multiple procedures. We can run one procedure from another.
Here is an example:
' https://excelmacromastery.com/ Sub Main() ' call each sub to perform a task CopyData AddFormulas FormatData End Sub Sub CopyData() ' Add code here End Sub Sub AddFormulas() ' Add code here End Sub Sub FormatData() ' Add code here End Sub
You can see that in the Main sub, we have added the name of three subs. When VBA reaches a line containing a procedure name, it will run the code in this procedure.
We refer to this as calling a procedure e.g. We are calling the CopyData sub from the Main sub.
There is actually a Call keyword in VBA. We can use it like this:
' https://excelmacromastery.com/ Sub Main() ' call each sub to perform a task Call CopyData Call AddFormulas Call FormatData End Sub
Using the Call keyword is optional. There is no real need to use it unless you are new to VBA and you feel it makes your code more readable.
If you are passing arguments using Call then you must use parentheses around them.
For example:
' https://excelmacromastery.com/ Sub Main() ' If call is not used then no parentheses AddValues 2, 4 ' call requires parentheses for arguments Call AddValues(2, 4) End Sub Sub AddValues(x As Long, y As Long) End Sub
How to Return Values From a Function
To return a value from a function you assign the value to the name of the Function. The following example demonstrates this:
' https://excelmacromastery.com/ Function GetAmount() As Long ' Returns 55 GetAmount = 55 End Function Function GetName() As String ' Returns John GetName = "John" End Function
When you return a value from a function you will obviously need to receive it in the function/sub that called it. You do this by assigning the function call to a variable. The following example shows this:
' https://excelmacromastery.com/ Sub WriteValues() Dim Amount As Long ' Get value from GetAmount function Amount = GetAmount End Sub Function GetAmount() As Long GetAmount = 55 End Function
You can easily test your return value using by using the Debug.Print function. This will write values to the Immediate Window (View->Immediate window from the menu or press Ctrl + G).
' https://excelmacromastery.com/ Sub WriteValues() ' Print return value to Immediate Window Debug.Print GetAmount End Sub Function GetAmount() As Long GetAmount = 55 End Function
Using Parameters and Arguments
We use parameters so that we can pass values from one sub/function to another.
The terms parameters and arguments are often confused:
- A parameter is the variable in the sub/function declaration.
- An argument is the value that you pass to the parameter.
' https://excelmacromastery.com/ Sub UsingArgs() ' The argument is 56 CalcValue 56 ' The argument is 34 CalcValue 34 End Sub ' The parameter is amount Sub CalcValue(ByVal amount As Long) End Sub
Here are some important points about parameters:
- We can have multiple parameters.
- A parameter is passed using either ByRef or ByVal. The default is ByRef.
- We can make a parameter optional for the user.
- We cannot use the New keyword in a parameter declaration.
- If no variable type is used then the parameter will be a variant by default.
The Format of Parameters
Subs and function use parameters in the same way.
The format of the parameter statement is as follows:
' All variables except arrays [ByRef/ByVal] As [Variable Type] ' Optional parameter - can only be a basic type [Optional] [ByRef/ByVal] [Variable name] As <[Variable Type] = ' Arrays [ByRef][array name]() As [Variable Type]
Here are some examples of the declaring different types of parameters:
' https://excelmacromastery.com/ ' Basic types Sub UseParams1(count As Long) End Sub Sub UseParams2(name As String) End Sub Sub UseParams3(amount As Currency) End Sub ' Collection Sub UseParamsColl(coll As Collection) End Sub ' Class module object Sub UseParamsClass(o As Class1) End Sub ' Variant ' If no type is give then it is automatically a variant Sub UseParamsVariant(value) End Sub Sub UseParamsVariant2(value As Variant) End Sub Sub UseParamsArray(arr() As String) End Sub
You can see that declaring parameters looks similar to using the Dim statement to declare variables.
Multiple Parameters
We can use multiple parameters in our sub/functions. This can make the line very long
Sub LongLine(ByVal count As Long, Optional amount As Currency = 56.77, Optional name As String = "John")
We can split up a line of code using the underscore (_) character. This makes our code more readable
Sub LongLine(ByVal count As Long _ , Optional amount As Currency = 56.77 _ , Optional name As String = "John")
Parameters With a Return Value
This error causes a lot of frustration with new users of VBA.
If you are returning a value from a function then it must have parentheses around the arguments.
The code below will give the “Expected: end of statement” syntax error.
' https://excelmacromastery.com/ Sub UseFunction() Dim result As Long result = GetValue 24.99 End Sub Function GetValue(amount As Currency) As Long GetValue = amount * 100 End Function
We have to write it like this
result = GetValue (24.99)
ByRef and ByVal
Every parameter is either ByRef or ByVal. If no type is specified then it is ByRef by default
' https://excelmacromastery.com/ ' Pass by value Sub WriteValue1(ByVal x As Long) End Sub ' Pass by reference Sub WriteValue2(ByRef x As Long) End Sub ' No type used so it is ByRef Sub WriteValue3(x As Long) End Sub
If you don’t specify the type then ByRef is the type as you can see in the third sub of the example.
The different between these types is:
ByVal – Creates a copy of the variable you pass.
This means if you change the value of the parameter it will not be changed when you return to the calling sub/function
ByRef – Creates a reference of the variable you pass.
This means if you change the value of the parameter variable it will be changed when you return to the calling sub/function.
The following code example shows this:
' https://excelmacromastery.com/ Sub Test() Dim x As Long ' Pass by value - x will not change x = 1 Debug.Print "x before ByVal is"; x SubByVal x Debug.Print "x after ByVal is"; x ' Pass by reference - x will change x = 1 Debug.Print "x before ByRef is"; x SubByRef x Debug.Print "x after ByRef is"; x End Sub Sub SubByVal(ByVal x As Long) ' x WILL NOT change outside as passed ByVal x = 99 End Sub Sub SubByRef(ByRef x As Long) ' x WILL change outside as passed ByRef x = 99 End Sub
The result of this is:
x before ByVal is 1
x after ByVal is 1
x before ByRef is 1
x after ByRef is 99
You should avoid passing basic variable types using ByRef. There are two main reasons for this:
- The person passing a value may not expect it to change and this can lead to bugs that are difficult to detect.
- Using parentheses when calling prevents ByRef working – see next sub section
A Little-Known Pitfall of ByRef
There is one thing you must be careful of when using ByRef with parameters. If you use parentheses then the sub/function cannot change the variable you pass even if it is passed as ByRef.
In the following example, we call the sub first without parentheses and then with parentheses. This causes the code to behave differently.
For example
' https://excelmacromastery.com/ Sub Test() Dim x As Long ' Call using without Parentheses - x will change x = 1 Debug.Print "x before (no parentheses): "; x SubByRef x Debug.Print "x after (no parentheses): "; x ' Call using with Parentheses - x will not change x = 1 Debug.Print "x before (with parentheses): "; x SubByRef (x) Debug.Print "x after (with parentheses): "; x End Sub Sub SubByRef(ByRef x As Long) x = 99 End Sub
If you change the sub in the above example to a function, you will see the same behaviour occurs.
However, if you return a value from the function then ByRef will work as normal as the code below shows:
' https://excelmacromastery.com/ Sub TestFunc() Dim x As Long, ret As Long ' Call using with Parentheses - x will not change x = 1 Debug.Print "x before (with parentheses): "; x FuncByRef (x) Debug.Print "x after (with parentheses): "; x ' Call using with Parentheses and return - x will change x = 1 Debug.Print "x before (with parentheses): "; x ret = FuncByRef(x) Debug.Print "x after (with parentheses): "; x End Sub Function FuncByRef(ByRef x As Long) x = 99 End Function
As I said in the last section you should avoid passing a variable using ByRef and instead use ByVal.
This means
- The variable you pass will not be accidentally changed.
- Using parentheses will not affect the behaviour.
ByRef and ByVal with Objects
When we use ByRef or ByVal with an object, it only affects the variable. It does not affect the actual object.
If we look at the example below:
' https://excelmacromastery.com/ Sub UseObject() Dim coll As New Collection coll.Add "Apple" ' After this coll with still reference the original CollByVal coll ' After this coll with reference the new collection CollByRef coll End Sub Sub CollByVal(ByVal coll As Collection) ' Original coll will still reference the original Set coll = New Collection coll.Add "ByVal" End Sub Sub CollByRef(ByRef coll As Collection) ' Original coll will reference the new collection Set coll = New Collection coll.Add "ByRef" End Sub
When we pass the coll variable using ByVal, a copy of the variable is created. We can assign anything to this variable and it will not affect the original one.
When we pass the coll variable using ByRef, we are using the original variable. If we assign something else to this variable then the original variable will also be assigned to something else.
You can see find out more about this here.
Optional Parameters
Sometimes we have a parameter that will often be the same value each time the code runs. We can make this parameter Optional which means that we give it a default value.
It is then optional for the caller to provide an argument. If they don’t provide a value then the default value is used.
In the example below, we have the report name as the optional parameter:
Sub CreateReport(Optional reportName As String = "Daily Report") End Sub
If an argument is not provided then name will contain the “Daily Report” text
' https://excelmacromastery.com/ Sub Main() ' Name will be "Daily Report" CreateReport ' Name will be "Weekly Report" CreateReport "Weekly Report" End Sub
The Optional parameter cannot come before a normal parameter. If you do this you will get an Expected: Optional error.
When a sub/function has optional parameters they will be displayed in square parentheses by the Intellisense.
In the screenshot below you can see that the name parameter is in square parentheses.
A sub/function can have multiple optional parameters. You may want to provide arguments to only some of the parameters.
There are two ways to do this:
If you don’t want to provide an argument then leave it blank.
A better way is to use the parameter name and the “:=” operator to specify the parameter and its’ value.
The examples below show both methods:
' https://excelmacromastery.com/ Sub Multi(marks As Long _ , Optional count As Long = 1 _ , Optional amount As Currency = 99.99 _ , Optional version As String = "A") Debug.Print marks, count, amount, version End Sub Sub UseBlanks() ' marks and count Multi 6, 5 ' marks and amount Multi 6, , 89.99 ' marks and version Multi 6, , , "B" ' marks,count and version Multi 6, , , "F" End Sub Sub UseName() ' marks and count Multi 12, count:=5 ' marks and amount Multi 12, amount:=89.99 ' marks and version Multi 12, version:="B" ' marks,count and version Multi 12, count:=6, version:="F" End Sub
Using the name of the parameter is the best way. It makes the code more readable and it means you won’t have error by mistakenly adding extra commas.
wk.SaveAs "C:Docsdata.xlsx", , , , , , xlShared wk.SaveAs "C:Docsdata.xlsx", AccessMode:=xlShared
IsMissing Function
We can use the IsMissing function to check if an Optional Parameter was supplied.
Normally we check against the default value but in certain cases we may not have a default.
We use IsMissing with Variant parameters because it will not work with basic types like Long and Double.
' https://excelmacromastery.com/ Sub test() ' Prints "Parameter not missing" CalcValues 6 ' Prints "Parameter missing" CalcValues End Sub Sub CalcValues(Optional x) ' Check for the parameter If IsMissing(x) Then Debug.Print "Parameter missing" Else Debug.Print "Parameter Not missing" End If End Sub
Custom Function vs Worksheet Function
When you create a function it appears in the function list for that workbook.
Have a look at the function in the next example.
Public Function MyNewFunction() MyNewFunction = 99 End Function
If you add this to a workbook then the function will appear in the function list. Type “=My” into the function box and the function will appear as shown in the following screenshot.
If you use this function in a cell you will get the result 99 in the cell as that is what the function returns.
Conclusion
The main points of this post are:
- Subs and macros are essentially the same thing in VBA.
- Functions return values but subs do not.
- Functions appear in the workbook function list for the current workbook.
- ByRef allows the function or sub to change the original argument.
- If you call a function sub with parentheses then ByRef will not work.
- Don’t use parentheses on sub arguments or function arguments when not returning a value.
- Use parentheses on function arguments when returning a value.
What’s Next?
Free VBA Tutorial If you are new to VBA or you want to sharpen your existing VBA skills then why not try out the The Ultimate VBA Tutorial.
Related Training: Get full access to the Excel VBA training webinars and all the tutorials.
(NOTE: Planning to build or manage a VBA Application? Learn how to build 10 Excel VBA applications from scratch.)
What is a Function?
A function is a piece of code that performs a specific task and returns a result. Functions are mostly used to carry out repetitive tasks such as formatting data for output, performing calculations, etc.
Suppose you are developing a program that calculates interest on a loan. You can create a function that accepts the loan amount and the payback period. The function can then use the loan amount and payback period to calculate the interest and return the value.
Why use functions
The advantages of using functions are the same as the ones in the above section on why use subroutines.
Rules of naming functions
The rules for naming functions as the same as the ones in the above section on rules for naming subroutines.
VBA Syntax for declaring Function
Private Function myFunction (ByVal arg1 As Integer, ByVal arg2 As Integer) myFunction = arg1 + arg2 End Function
HERE in the syntax,
Code | Action |
---|---|
|
|
|
|
|
|
|
|
Function demonstrated with Example:
Functions are very similar to the subroutine. The major difference between a subroutine and a function is that the function returns a value when it is called. While a subroutine does not return a value, when it is called. Let’s say you want to add two numbers. You can create a function that accepts two numbers and returns the sum of the numbers.
- Create the user interface
- Add the function
- Write code for the command button
- Test the code
Step 1) User interface
Add a command button to the worksheet as shown below
Set the following properties of CommanButton1 to the following.
S/N | Control | Property | Value |
---|---|---|---|
1 | CommandButton1 | Name | btnAddNumbers |
2 | Caption | Add Numbers Function |
Your interface should now appear as follows
Step 2) Function code.
- Press Alt + F11 to open the code window
- Add the following code
Private Function addNumbers(ByVal firstNumber As Integer, ByVal secondNumber As Integer) addNumbers = firstNumber + secondNumber End Function
HERE in the code,
Code | Action |
---|---|
|
|
|
|
|
|
Step 3) Write Code that calls the function
- Right click on btnAddNumbers_Click command button
- Select View Code
- Add the following code
Private Sub btnAddNumbersFunction_Click() MsgBox addNumbers(2, 3) End Sub
HERE in the code,
Code | Action |
---|---|
“MsgBox addNumbers(2,3)” |
|
Step 4) Run the program, you will get the following results
Download Excel containing above code
Download the above Excel Code
Summary:
- A function is a piece of code that performs a specific task. A function returns a value after execution.
- Both subroutines and functions offer code reusability
- Both subroutines and functions help break down large chunks of code into small manageable code.