PDF download Download Article PDF download Download Article

Are you confused about calling function procedures in Visual Basic and VBA? This wikiHow tutorial will teach you easy ways to call functions in VB and VBA.

Section 1 of 2:

Calling a Function Syntax

PDF download Download Article
  1. The syntax for calling a function in VB and VBA is lvalue = functionName (argument1, argument2) . If there is more than one argument (like in this example), you'll separate the arguments with commas. [1]
    • You must provide values for all arguments that aren't optional. But if there are no arguments, you can omit the parentheses or just leave them blank () . [2]
    • When you call a function, its statements run beginning with the first executable statement after the Function statement until End Function , Return , or Exit Function is encountered. [3]
    • Unlike when calling a subroutine, you usually won't need to include the Call keyword when calling a function. [4] If you use the Call keyword, no value will be returned.
    • If you're using VBA in a cell within an Excel spreadsheet, the syntax is =functionName(argument1, argument2) . If you're writing code in the Excel VB editor, you'll use the standard syntax. But in a cell, you'll need to preface the call with an equals sign as you would when writing a formula.
  2. Advertisement
Section 2 of 2:

Function Calling Examples

PDF download Download Article
  1. [5] Then, we'll call that function to find the hypotenuse for a triangle if one of the lines is 2.3 .
    • First, let's create the function.
       Function 
       hypotenuse 
       ( 
       ByVal 
       side1 
       As 
       Single 
       , 
       ByVal 
       side2 
       As 
       Single 
       ) 
       As 
       Single 
       Return 
       Math 
       . 
       Sqrt 
       (( 
       side1 
       ^ 
       2 
       ) 
       + 
       ( 
       side2 
       ^ 
       2 
       )) 
       End 
       Function 
      
    • And here's how we'd call the function if one side is 2.3 .
       Dim 
       testLength 
       , 
       testHypotenuse 
       As 
       Single 
       testHypotenuse 
       = 
       hypotenuse 
       ( 
       testLength 
       , 
       2.3 
       ) 
      
  2. In this example, we'll call the MsgBox function, which displays a message box to the user and accepts input. There are 5 possible named arguments for MsgBox in Visual Basic and VBA— prompt , buttons , title , helpfile , and context . [6] We want to pass two arguments—one to display a message, and another to display yes and no buttons:
     Dim 
     intResponse 
     As 
     Integer 
     intResponse 
     = 
     MsgBox 
     ( 
     "Are you sure you want to proceed?" 
     , 
     vbYesNo 
     ) 
     If 
     intResponse 
     = 
     vbYes 
     Then 
     End 
     End 
     If 
    
  3. First we'll write a function called Add , and then we'll call the function to add the numbers 32 and 64 .
    • First, here's the function: [7]
       Function 
       Add 
       ( 
       ByVal 
       x 
       As 
       Integer 
       , 
       ByVal 
       y 
       As 
       Integer 
       ) 
       As 
       Integer 
       Dim 
       Res 
       as 
       integer 
       Res 
       = 
       x 
       + 
       y 
       Add 
       = 
       Res 
       End 
       Function 
      
    • Now, let's call the function in a subroutine to add our numbers:
       Sub 
       Form_Load 
       () 
       Dim 
       a 
       As 
       Integer 
       Dim 
       b 
       As 
       Integer 
       Dim 
       c 
       As 
       Integer 
       a 
       = 
       32 
       b 
       = 
       64 
       c 
       = 
       Add 
       ( 
       a 
       , 
       b 
       ) 
       MsgBox 
       ( 
       "Sum is : " 
       & 
       c 
       ) 
       End 
       Sub 
      
  4. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How do I run a function in VB.net?
    Snod
    Community Answer
    Click Compile then Run using the menu. There may also be keyboard shortcut hotkeys.
Ask a Question
      Advertisement

      Tips

      • Functions are public by default, so you can call them from anywhere in the program that has access to its containing class, module, or structure.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 164,304 times.

      Is this article up to date?

      Advertisement