[RESOLVED] Function questions (Excel VBA)
Why is that that I can call a function with one input, yet when I have a function with more than 1 something has to be equal to it?
For example:
VB Code:
Sub FunctionTest()
Dim BigNum As Integer
Dim SmallNum As Integer
Dim Output1 As Long
Dim Output2 As Long
'set variable values
BigNum = 15
SmallNum = 5
'call functions
DivBy3 (BigNum)
'DivBy3(BigNum,SmallNum) <--- This yields "Compile Error : Syntax Error"
Test = DivByB(BigNum, SmallNum)
End Sub
Function DivBy3(In_A As Integer)
DivBy3 = In_A / 3
End Function
Function DivByB(In_A As Integer, In_B As Integer)
DivByB = In_A / In_B
End Function
In that example, the function will run for the 1st and third, but not the one commented out. A lot of times, I need the opposite, though.
I set a single variable function equal to something, and I use a dummy variable for the second function, because instead of doing simple math, I'm having the function run other things in the backround.
I just need the function to run and do it's own thing based on the inputs, but I don't need a variable equal to the function.
Am I going about this the wrong way, or is this just how it is?
Thanks for any assistance.
--Fizziii
Re: Function questions (Excel VBA)
you can only pass the number of arguments the function takes, if you try to pass more or less then you will get an error
the only time you can vary the number of arguments is if the function has optional arguments, the you can pass those or not, but the function has to know how to handle the argumant if is /is not passed
arguments should be enclosed in bracket if there is an equals
mysub arg1, arg2
test = myfunc(arg1, arg2)
functions should be equal to something as the point of them is to have a return value,
if no return value then it should be a sub
Re: Function questions (Excel VBA)
WestConn, thank you. You confirmed my suspicions. It was a PEBKAC (Problem Exists Between Keyboard And Chair) issue.