General VBA Function Question

kevygee

Registered User.
Local time
Today, 05:06
Joined
Jun 7, 2005
Messages
32
I think I might have an easy question for people who have programmed with VBA before. I just started (about 2 hours ago) programming with VBA. I know other coding languages. And here's my problem, I wanted to write a user-defined function, that compartmentalized a common task and it required no arguments. So for example:

Code:
Function commonTask()
     do.Common.stuff
End Function

Easy enough. But when I put a call to this function, like so:
Code:
commonTask()
VBA returns a general error telling me I'm wrong.

Now, I changed all this to
Code:
Function commonTask(dummyString as String)
     do.Common.stuff
End Function

and the call as such:
Code:
commonTask("dummyString")

and voila! it worked. Am I missing something? Do I have to include some null or void keyword indicating I don't want to pass anything? I feel that I shouldn't have to pass a variable through.

Also, if you know of any good online VBA tutorials, I would be more than happy to read it. Thanks a lot in advance.

Kevygee
 
You might be confusing a SUB and FUNCTION
The usage you have is typical of a SUB.

A function usually has a return datatype and assigns that to it's return

FUNCTION DoSomething() As Boolean
Some Code
DoSomething = True
END FUNCTION

would be called as a function
MyBooleanVar = DoSomething()

OR
If DoSomething() then
true stuff
ELSE
false stuff
END IF

A sub on the other hand does not return a value.

SUB DoSomething()
Code Stuff
END SUB

A sub is typically called Like
DoSomething
or if there are parameters, DoSomething (parameters)
 
What is "do.Common.stuff"?

No, you don't need to include a Void, but it is good practice to display the scope of the function (private/public).

Also, when calling, if there are no parameters to pass in the function, you do not need to include the parentheses. The only time you need the parentheses is when you're returning the value to a variable and using the equality operator. For instance:
someVariable = commonTask(parameter)

No parameter:
someVariable = commonTask

Calling the function by itself
commonTask
-or-
Call commonTask



------
Additionally,
If the function isn't returning a value, make it a sub. The only time you make a routine that doesn't return a value a "function" is when you wish to use it in a macro. I hope this helps,
Modest
 
Great. My problem was that I was using the parentheses when calling the function that didn't require arguments. Also thanks a lot for the sub vs function tip. I was trying to look up the difference online and I really couldn't find anything explaining it. Probably because they operate very similarly, but the usage is for two different things.

Thanks a lot. The solution to this problem has to led to many other problems. I guess that's known as progress, haha.
 
I prefer to stick the Call statement in front of my subs and functions so I can "see" where I am calling another function or sub. Here is a quote from the help files...


Call Statement


Transfers control to a Sub procedure, Function procedure, or dynamic-link library (DLL) procedure.

Syntax

[Call] name [argumentlist]

The Call statement syntax has these parts:

Part Description
Call Optional; keyword. If specified, you must enclose argumentlist in parentheses. For example:
Call MyProc(0)
name Required. Name of the procedure to call.
argumentlist Optional. Comma-delimited list of variables, arrays, or expressions to pass to the procedure. Components of argumentlist may include the keywords ByVal or ByRef to describe how the arguments are treated by the called procedure. However, ByVal and ByRef can be used with Call only when calling a DLL procedure. On the Macintosh, ByVal and ByRef can be used with Call when making a call to a Macintosh code resource.



Remarks

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

To pass a whole array to a procedure, use the array name followed by empty parentheses.

 

Users who are viewing this thread

Back
Top Bottom