|
-
Aug 21st, 2008, 04:26 PM
#1
Thread Starter
Addicted Member
[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
-
Aug 21st, 2008, 04:52 PM
#2
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
Last edited by westconn1; Aug 21st, 2008 at 04:55 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 21st, 2008, 05:38 PM
#3
Thread Starter
Addicted Member
Re: Function questions (Excel VBA)
WestConn, thank you. You confirmed my suspicions. It was a PEBKAC (Problem Exists Between Keyboard And Chair) issue.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|