With just one argument, VBA does let you get away with using the
parenthesizes. However, it puts a space between the method name and the
opening parenthesis.
Okay, so with the Call statement, you do have to use parenthesizes as I
didn't realize that without looking in the help file, so you got me on
that one. However, without the Call keyword, if attempt to use a
function as a Sub with the parenthesizes that involves more than 1
argument, it will give you a syntax error.
As for what you are implying with the "SheSaid" function, you are saying
that you can't use Functions as Subs, which if that is the case, you are
incorrect there as well. Functions can be setup either to return values
or to perform executions and return a value to indicate what ever it's
intended to indicate. In the latter case, the developer that uses the
function can then either use it as a sub or as a function (normally for
error trapping purposes, but can be for other purposes as well such as the
MsgBox function can be used either as a sub [for simple information for
the user] or a function [for the user to choose which way to go, which
then the code continues with which ever direction based on which button
the user activated]).
As for the line:
What are you attempting to say here? Of course you can't use a function
as a sub in this manner. For that case, you can't use a Sub in this
manner either as a Sub doesn't return anything.
And this line below as well:
MsgBox SheSaid "This errs"
Again, this is an obvious syntax issue as there's too many issues with it.
I'm not going to get into the list of issues as that can get to be rather
lengthy.
As for this line below of yours
MessageMe "First message", 1
Yes, you can use a function like this. You are using like a sub as you
are not using the value it returns. It may return the value, but it's not
being assigned to anything so it's value is almost instantly forgotten.
As for the line below:
Call SheSaid("You won't see this")
You are attempting to say you can't use this as a Sub. With this use of a
Function of course not cause it doesn't perform any sort of action outside
of assignments. If you are using a Function that performs other actions,
and returns a Boolean value to indicate rather if it was successful or not
(Just one example of how something like that could be setup), then the
developer can either use it like a sub and ignore the result from the
function or like an actual function call taking into account of the result
from the function. On the other hand, if you are attempting to use this
as a function, then it's an improper syntax for that.
Out of all the lines of code you have provided, you still haven't provided
a good reason why one must use a Call statement. On the other hand, if
one is using it just as a preference, no problem there as we each have our
own systems, what we are familiar with and use to. I don't know if you
misinterpreted my statements about Subs and Functions, but the only thing
I was incorrect on initially was in regards to the parenthesizes on a Call
statement for sub use. I admit I haven't used the Call statement, but I
have used and still do use Subs and Functions quite extensively including
in classes just as I do with many of the other key components. If I
didn't know how to use Subs and Functions by now, I wouldn't be nearly as
far along as I am.
--
Thanks,
Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
Harald Staff said:
Hi Ronald
As stated, this is only about making the code readable and self
explaining. I hate writing code documentation.
You are not right about sub and function calls. Try this:
Sub test()
Dim S As String
' sub calls:
MessageMe "First message", 1
Call MessageMe("Second message", 2)
'function calls:
MsgBox SheSaid("Watch this")
S = SheSaid("And this")
MsgBox S
Call SheSaid("You won't see this")
MsgBox SheSaid "This errs"
S = SheSaid "This too"
MsgBox S
End Sub
Sub MessageMe(strMsg As String, LCounter As Long)
MsgBox strMsg & vbNewLine & vbNewLine & _
"This is example number " & LCounter
End Sub
Function SheSaid(strMsg As String) As String
SheSaid = "The lady said: " & strMsg
End Function
A sub is just a function returning nothing (Void), so from a code point
of view the difference is very little.
Best wishes Harald
Ronald R. Dodge said:
What sort of issues have you had without using the Call keyword? I been
doing it without the Call keyword from the start of me using VBA back in
1998 and never once have I had a problem without using the Call keyword.
The only thing I see based on how you using the 2 lines below, the first
line is calling up the command as if it's being used as a "Sub" call
(Rather if the method is a Sub or Function) vs. the second one is being
used as a "Function" call (The method is a Function). How can I tell?
First call (implied), you don't use the parenthesizes and the second
call, you do.
Sub calls, you can only use without the parenthesizes vs. Function
calls, you can use the parenthesizes, but don't have to. However, that
depends if you are using the method as a function to return something
(Use the parenthesizes) or if you are using it as a sub just to execute
something (Don't use the parenthesizes).
However, if you do use the parenthesizes, you must have it either equal
to something (via either Let [in many cases, you see this left off too,
but can be used] or Set) or have it in an argument that will take the
value/object of what the function returns.
--
Thanks,
Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
I'm with Susan on this, code is unreadable without Call.
And it gets a lot worse when passing variables to code:
DoSomethingUseful X, MyString, MyCell, strWarningText
vs
Call DoSomethingUseful (X, MyString, MyCell, strWarningText)
Best wishes Harald
"Ronald R. Dodge, Jr." <
[email protected]> skrev i melding
Actually, you don't even have to use the "Call" keyword as it's
assumed in VBA.