Store into variable result of function VBA

Victor

I have a function that makes a particular calculation. Then, I have a subroutine from which I want to call the function and make some stuff with it. I believe I should be returning a value ("counter") at the end of the function and then storing it on the sub, but how do I do that?

Function thisFunction(int1 As Integer, int2 As Integer)

    Dim counter As Integer
    counter = 0

    Dim i As Integer
    For i = 1 To 10
        counter = int1 + int2
    Next

End Function

Sub getResult()

    Dim result As Integer
    result = thisFunction(5, 2)
    MsgBox (result)

End Sub
GELR

In VBA, you don't return result with a return statement. You assign the value to the function name. The function does not have to be typed with the "As Integer" by default it will return as variable. So modifying your sample just this way will make it work.

Function thisFunction(int1 As Integer, int2 As Integer)

    Dim counter As Integer
    counter = 0

    Dim i As Integer
    For i = 1 To 10
        counter = int1 + int2
    Next

    thisFunction = counter
End Function

Sub getResult()

    Dim result As Integer
    result = thisFunction(5, 2)
    MsgBox (result)

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Store function result into variable

From Dev

Store function result into variable

From Dev

Store function result to variable for later use in PHP?

From Dev

How can I store the result of a function into a variable?

From Dev

PHP: Store Result of JavaScript Function into a PHP Variable

From Dev

How can I store a result of a function in a variable in Python?

From Dev

Why store the result of getElementById() to a variable instead of using the function everytime?

From Dev

How can I store a result of a function in a variable in Python?

From Dev

Store column result in variable

From Dev

Store the result of a command in a variable

From Dev

store into variable a sudo result

From Dev

VBA store msoThemeColor in variable

From Dev

Cannot store result into variable SQL

From Dev

Store the assert failure result in a variable

From Dev

Store the result of a Dynamic Query in a variable

From Dev

Store cmd Find result to a variable

From Dev

Sed Fails to store the result into a variable

From Dev

Store Result of SVN Command in Variable

From Dev

How to store a query result in a variable

From Dev

Store result of a query inside a function

From Dev

Assign the result of a function to variable?

From Dev

Python - Store function in variable

From Dev

vba store recordset as integer variable

From Dev

Laravel: Return a database query result using the MySQL SUM function and store it in a variable

From Dev

excel VBA return result of function

From Dev

signed result of Val function in VBA

From Dev

VBA function result type mismatch

From Dev

Grep a variable and store the result in a vector in R

From Dev

Is it okay to store the result of a JQuery selector in a variable?

Related Related

HotTag

Archive