This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.
This article has been fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources.
This article has been viewed 167,433 times.
Learn more...
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.
Calling a Function in VBA: Overview
Use the syntax lvalue = functionName (argument1, argument2) to call functions in VB and VBA. If there are no arguments, omit the parentheses or leave them blank. If you're using VBA within a cell in an Excel spreadsheet, use the syntax =functionName(argument1, argument2).
Steps
Function Calling Examples
-
In this example, we'll write a function that finds the hypotenuse of a right triangle.[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)
- First, let's create the function.
-
We can also call functions using expressions. 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
-
Now we'll create a function that adds two numbers together. 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
- First, here's the function:[7]
Community Q&A
-
QuestionHow do I run a function in VB.net?SnodCommunity AnswerClick Compile then Run using the menu. There may also be keyboard shortcut hotkeys.
Video
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.Thanks
References
- ↑ https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/call-statement
- ↑ https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/function-procedures
- ↑ https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/function-procedures
- ↑ https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/function-statement
- ↑ https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/function-procedures
- ↑ https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/msgbox-function
- ↑ https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/add-method-visual-basic-for-applications