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
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.
Comments