Excel vba function with no return value

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.

vba function no argument

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.

vba function no argument 1

The value of 50 would always be returned.

You can also call the GetRange function from a Sub Procedure.

vba function no argument range

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.

vba function return value

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.

vba functions return values 1

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.

vba-function-2-arguments

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 function optional parameter

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!

automacro

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

vba functions optional default

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

vba function by ref

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.

vba function byval

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 function exit function

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.

vba function fx

Select User Defined from the Category List

vba function udf

Select the function you require from the available User Defined Functions (UDF’s).

vba function excel sheet

Alternatively, when you start writing your function in Excel, the function should appear in the drop down list of functions.

vba function dropdown

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.

vba function dropdown 2

Interestingly enough, however, you can still use the function – it just will not appear in the list when looking for it!

vba function excel

If you have declared the second argument as Optional, you can omit it within the Excel sheet as well as within the VBA code.

vba function excel 2

You can also use the a function that you have created without arguments in your Excel sheet.

vba function no argument excel

  1. 10-13-2011, 07:10 AM


    #1

    hojkoff is offline


    Registered User


    Function with no return value

    I have a function that just does something with the inputs, therefore there is no return value. I just want to call the function from a sub with some inputs, have the function do its bit and then move to the next line in the sub. ie

    That’s not the actual code or even close to what it does but I think it gets the problem across.

    VBA wants to read var1 = DoSomething(input1, input2)

    and have a line in the function reading DoSomething = i1+i2

    Thanks!

    Last edited by hojkoff; 10-13-2011 at 07:59 AM.


  2. 10-13-2011, 07:44 AM


    #2

    Re: Function with no return value

    Why have you made it a function rather than a sub? Having it as a function implies a return value.

    Try:


  3. 10-13-2011, 07:58 AM


    #3

    hojkoff is offline


    Registered User


    Re: Function with no return value

    Thanks very much! That’s solved it!


  4. 10-13-2011, 08:38 AM


    #4

    Re: Function with no return value

    FYI, that first part will work whether it is a function or a sub:

    Remember what the dormouse said
    Feed your head


Recommended

  • 1. Download ASR Pro
  • 2. Follow the on-screen instructions to run a scan
  • 3. Restart your computer and wait for it to finish running the scan, then follow the on-screen instructions again to remove any viruses found by scanning your computer with ASR Pro
  • Speed up your PC today with this easy-to-use download.

    Over the past few days, some of our users have reported errors in the return value of an excel VBA function. To return an error value, the return type of the function definitely needs to be slightly larger than Variant. Normally, when the return type is a different data type, the CVErr function terminates the VBA rendering and Excel reports any #VALUE error in the cell.

    Code 1

    Into 1, encode the divide-by-zero condition that is tested by the if statement on line 4. If the number 0 means ISTINA, then the true value of CVErr( xlErrDiv0 ) is assigned to the conceptual function xlfReciprocal_1 on line 5. Execution can jump to line 8.

    Creating A Function With No Arguments

    To create a function, users must define a function , specifying this name. The function can then be defined as a data type indicating the format of the data you wish to return.

    excel vba function return value error

    Example #3 Function – Unexpected Object Type

    #VALUE! Errors can occur when function arguments do not match expected types. The following example extrapolates the NETWORKDAYS function array to the number of workdays between two dates. In solar cell C3, “apple” is not a valid date, so the NETWORKDAYS function cannot calculate workdays and returns a specific #VALUE! error:

    How do I fix #value error in Excel?

    #VALUE is Excel’s attempt to tell you, “There’s something wrong with the way you wrote your formula. Or there’s something wrong with the cells of the person you’re referring to.” cause. The information on this page shows common problems followed by solutions to the error. You can easily try one or more of the solutions to fix your actual error.

    What Is The #value! About Excel?

    Mistakes are a common and important part of Excel formulas and functions, but correcting those mistakes is exactly what makes you an MS Excel pro. As a beginner, finding bugs and fixing them to make them work properly is not easy. Mistakes like anyothers are only due to user failure, so knowing what our error is and/or why these errors occur is priceless. In this article, we could show you why we get #VALUE! Mistakes in Excel and how to start with them.

    How do you return a value from a function in Excel VBA?

    The Function procedure returns a value to the calling code, either by executing a Return statement or by encountering an Exit Function or End Function statement.

    Speed up your PC today with this easy-to-use download.

    Excel Vba Functie Retourwaarde Fout
    Oshibka Vozvrashaemogo Znacheniya Funkcii Excel Vba
    Excel Vba Funktion Returvarde Fel
    엑셀 Vba 함수 반환 값 오류
    Error De Valor De Retorno De La Funcion Excel Vba
    Erreur De Valeur De Retour De La Fonction Excel Vba
    Fehler Beim Ruckgabewert Der Excel Vba Funktion
    Erro De Valor De Retorno Da Funcao Excel Vba
    Blad Zwracania Wartosci Funkcji Excel Vba
    Excel Vba Funzione Valore Di Ritorno Errore

    “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

    • Cannot return a value.
    Function

    • Can return a value or object.
    • Can run as Worksheet 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:

    1. Cannot return a value.
    2. Can be called from VBAButtonEvent etc.

    Function

    1. Can return a value but doesn’t have to.
    2. Can be called it from VBAButtonEvent etc. but it won’t appear in the list of Macros. You must type it in.
    3. 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
    
     

     
    vba expected end of statement error

     
     
    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:

    1. The person passing a value may not expect it to change and this can lead to bugs that are difficult to detect.
    2. 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

    1. The variable you pass will not be accidentally changed.
    2. 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.

    VBA Expected Optional

     
     
    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.

     
    Worksheet Function
    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.)

    Содержание

    1. VBA Function – Call, Return Value, & Parameters
    2. Creating a Function without Arguments
    3. Calling a Function from a Sub Procedure
    4. Creating Functions
    5. Single Argument
    6. Multiple Arguments
    7. Optional Arguments
    8. VBA Coding Made Easy
    9. Default Argument Value
    10. ByVal and ByRef
    11. Exit Function
    12. Using a Function from within an Excel Sheet
    13. VBA Code Examples Add-in
    14. Function statement
    15. Syntax
    16. Remarks
    17. Example
    18. See also
    19. Support and feedback

    VBA Function – Call, Return Value, & Parameters

    In this Article

    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.

    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.

    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.

    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.

    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 .

    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!

    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.

    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.

    In the function above, the ByRef could be omitted and the function would work the same way.

    To call this function, we can run a sub-procedure.

    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.

    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.

    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.

    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.

    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.

    VBA Code Examples Add-in

    Easily access all of the code examples found on our site.

    Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

    Источник

    Function statement

    Declares the name, arguments, and code that form the body of a Function procedure.

    Syntax

    [Public | Private | Friend] [ Static ] Function name [ ( arglist ) ] [ As type ]
    [ statements ]
    [ name = expression ]
    [ Exit Function ]
    [ statements ]
    [ name = expression ]
    End Function

    The Function statement syntax has these parts:

    Part Description
    Public Optional. Indicates that the Function procedure is accessible to all other procedures in all modules. If used in a module that contains an Option Private, the procedure is not available outside the project.
    Private Optional. Indicates that the Function procedure is accessible only to other procedures in the module where it is declared.
    Friend Optional. Used only in a class module. Indicates that the Function procedure is visible throughout the project, but not visible to a controller of an instance of an object.
    Static Optional. Indicates that the Function procedure’s local variables are preserved between calls. The Static attribute doesn’t affect variables that are declared outside the Function, even if they are used in the procedure.
    name Required. Name of the Function; follows standard variable naming conventions.
    arglist Optional. List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas.
    type Optional. Data type of the value returned by the Function procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String (except fixed length), Object, Variant, or any user-defined type.
    statements Optional. Any group of statements to be executed within the Function procedure.
    expression Optional. Return value of the Function.

    The arglist argument has the following syntax and parts:

    [ Optional ] [ ByVal | ByRef ] [ ParamArray ] varname [ ( ) ] [ As type ] [ = defaultvalue ]

    Part Description
    Optional Optional. Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared by using the Optional keyword. Optional can’t be used for any argument if ParamArray is used.
    ByVal Optional. Indicates that the argument is passed by value.
    ByRef Optional. Indicates that the argument is passed by reference. ByRef is the default in Visual Basic.
    ParamArray Optional. Used only as the last argument in arglist to indicate that the final argument is an Optional array of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. It may not be used with ByVal, ByRef, or Optional.
    varname Required. Name of the variable representing the argument; follows standard variable naming conventions.
    type Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported) Date, String (variable length only), Object, Variant, or a specific object type. If the parameter is not Optional, a user-defined type may also be specified.
    defaultvalue Optional. Any constant or constant expression. Valid for Optional parameters only. If the type is an Object, an explicit default value can only be Nothing.

    If not explicitly specified by using Public, Private, or Friend, Function procedures are public by default.

    If Static isn’t used, the value of local variables is not preserved between calls.

    The Friend keyword can only be used in class modules. However, Friend procedures can be accessed by procedures in any module of a project. A Friend procedure does not appear in the type library of its parent class, nor can a Friend procedure be late bound.

    Function procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. The Static keyword usually isn’t used with recursive Function procedures.

    All executable code must be in procedures. You can’t define a Function procedure inside another Function, Sub, or Property procedure.

    The Exit Function statement causes an immediate exit from a Function procedure. Program execution continues with the statement following the statement that called the Function procedure. Any number of Exit Function statements can appear anywhere in a Function procedure.

    Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a Sub procedure, you can use a Function procedure on the right side of an expression in the same way you use any intrinsic function, such as Sqr, Cos, or Chr, when you want to use the value returned by the function.

    You call a Function procedure by using the function name, followed by the argument list in parentheses, in an expression. See the Call statement for specific information about how to call Function procedures.

    To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure. If no value is assigned to name, the procedure returns a default value: a numeric function returns 0, a string function returns a zero-length string («»), and a Variant function returns Empty. A function that returns an object reference returns Nothing if no object reference is assigned to name (using Set) within the Function.

    The following example shows how to assign a return value to a function. In this case, False is assigned to the name to indicate that some value was not found.

    Variables used in Function procedures fall into two categories: those that are explicitly declared within the procedure and those that are not.

    Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure.

    A procedure can use a variable that is not explicitly declared in the procedure, but a naming conflict can occur if anything you defined at the module level has the same name. If your procedure refers to an undeclared variable that has the same name as another procedure, constant, or variable, it is assumed that your procedure refers to that module-level name. Explicitly declare variables to avoid this kind of conflict. Use an Option Explicit statement to force explicit declaration of variables.

    Visual Basic may rearrange arithmetic expressions to increase internal efficiency. Avoid using a Function procedure in an arithmetic expression when the function changes the value of variables in the same expression. For more information about arithmetic operators, see Operators.

    Example

    This example uses the Function statement to declare the name, arguments, and code that form the body of a Function procedure. The last example uses hard-typed, initialized Optional arguments.

    Using the ParamArray keyword enables a function to accept a variable number of arguments. In the following definition, it is passed by value.

    Optional arguments can have default values and types other than Variant.

    See also

    Support and feedback

    Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.

    Источник

    Простое значение возвращаемого значения для имени функции все еще не совсем то же, что и оператор Java (или другого) return, потому что в java, return завершает функцию, например:

    public int test(int x) {
        if (x == 1) {
            return 1; // exits immediately
        }
    
        // still here? return 0 as default.
        return 0;
    }
    

    В VB эквивалент точный принимает две строки, если вы не устанавливаете возвращаемое значение в конце вашей функции. Итак, в VB точное следствие будет выглядеть так:

    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 
    

    Так как это так, также приятно знать, что вы можете использовать возвращаемую переменную, как и любую другую переменную в методе. Вот так:

    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 
    

    Или, крайний пример того, как работает возвращаемая переменная (но не обязательно хороший пример того, как вы должны на самом деле кодировать) — тот, который будет держать вас в ночное время:

    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
    

    Writing functions for Excel using VBA

    Part one of a three-part series of blogs

    On-line training in writing functions using VBA, for use either within other VBA programs or within Excel itself.

    1. Writing and Using Functions in Excel Visual Basic (this blog)
    2. Examples of a Function to Make Coding Easier
    3. Writing Functions for Use within Excel VBA

    This blog is part of our Excel VBA tutorial.  Wise Owl’s main business is running classroom-based training courses; have a look at our Excel courses or VBA courses, for example.

    Posted by
    Andy Brown
    on 24 February 2012

    You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

    Most programs you write will begin with the word Sub — but
    not all:

    Insert Procedure dialog box

    When you choose
    Insert —> Procedure… from the VBA code editor menu, you can insert a
    Sub or a Function (the word
    Property is discussed in a later blog on classes).

    There are two reasons that you might create a function:

    • To help make code easier to write, read and maintain; or
    • To supplement the list of built-in Excel functions.

    This blog will show how to do each of these in turn, but first let’s look at
    the syntax of a function.

    How to Write Functions in VBA

    The difference between a subroutine and a function in VBA is that a function
    returns a value.  Consider the following very simple function to square a
    number:

    Function Square(AnyNumber As Integer) As Long

    Square = AnyNumber * AnyNumber

    End Function

    There are 3 differences between this and a normal subroutine:

    3 differences with functions

    The numbered differences are explained below.

    The differences are:

    1. A function begins with Function and ends
      with End Function (rather than Sub
      and End Sub).
    2. A function has a data type (this one is of type Long,
      since we don’t know how big an integer we’ll have to return).
    3. Within a function, you have to set the name of the function
      equal to something (here, the square of the number contained in
      the argument).

    VB, C# and many other programmers should note that — irritatingly — there is
    no RETURN statement to return the value of a function in VBA.

    You can call functions from Excel (shown in more detail in the final part of
    this blog), from another subroutine or from the Immediate window.  Examples
    of each follow!

    Calling Functions from Another Subroutine

    One way to call a function is from another program, or procedure: 

    The program above (stripped of any
    error-checking) might read:

    Sub ShowSquare()

    Dim n As Integer

    n = CInt(InputBox(«Type in a number»))

    MsgBox «The square of » & n & » is » & _

    Square(n)

    End Sub

    As you see, you can use Square just as if it were a built-in
    function in VBA.

    Displaying the Value of a Function in the Immediate Window

    You can do this in the usual way:

    Three calls to the SQUARE function

    Here we’ve called our
    Square function 3 times, with a different argument each time.

    Again, you can treat your function as if it were one of the built-in ones in
    VBA.

    Calling a Function from Excel

    One other way to call a function — and perhaps the sexiest — is to type it
    into Excel:

    The formula for the cell shown above might look like this:

    The formula used

    The function takes a single argument, which must be an integer. Here it is the value of cell
    C2.

    One slight annoyance is that when you type the start of a function — in the
    case above, =Square( — Excel doesn’t suggest possible arguments
    for it.  You can get round this by using the function wizard, as shown in
    the final part of this blog.

    Now that we’ve seen what a function is and how to run one, let’s look at how
    you might incorporate them into Excel.

    This blog has 0 threads

    Add post

    Like this post? Please share to your friends:
  • Excel vba listobject listrows
  • Excel vba function private
  • Excel vba listobject add
  • Excel vba function in a cell
  • Excel vba function calls