Kamis, 13 Desember 2012

RE: [ExcelVBA] Using Parentheses in Subroutine Calls

 

Interesting, I've never noticed that. (I insist on the parentheses in
my code, so always use Call.)

You get a syntax error when you compile the two parameter Sub reference.
However, when you type it in (in 2003), you get "Expected: =". Looks as
though it's syntax-checking my "bb(1, 2)" as an index into an array bb,
even though there isn't one, and there's definitely a Sub bb.
Interestingly, this happens even if Sub bb appears in the code of the
module before the call to it.

I changed the statement to "bb(1, 2) = 1" and this generates an
"Expected Function or variable" error. So it could also be expecting bb
to be a Function, returning a Variant or Object, that would allow it to
be used on the left of the equals.

> Does anyone know why the single argument works
> with a parentheses?

Or the converse, why you can't do it for multiple arguments ...

Flaky syntax analysis, I suspect. I wonder if it's fixed in 2007 or
2010.

==================================================

Aha ... just found something in
http://stackoverflow.com/questions/479891/what-does-the-call-keyword-do-
in-vb6

Try the following code

Option Explicit

Sub tester()
Dim a As String: a = "Original"
Call Test(a)
Dim b As String: b = "Original"
Test b
Dim c As String: c = "Original"
Test (c)
Range("A1").Value = "a = " & a & ", b = " & b & ", c = " & c
End Sub

Sub Test(ByRef param As String)
param = "Test changed it"
End Sub

Result is: a = Test changed it, b = Test changed it, c = Original

so we have it.

The call with no Call and the single parameter in parentheses is
evaluating the (c) as a string expression. The call with a Call passes
a as a byref variable. The call with no Call and no parentheses b as a
byref variable.

So my "bb(1, 2)" fails because it attempts to evaluate "(1, 2)" as a
string expression, which is bad syntax. It possibly also attempts to
interpret "bb(1, 2)" as an array reference and also as a function call,
but it gives up and can't decide what the problem is ... hence "Syntax
error".

Regards, Dave S

________________________________

From: ExcelVBA@yahoogroups.com [mailto:ExcelVBA@yahoogroups.com] On
Behalf Of mickey11030
Sent: Friday, 14 December 2012 13:18
To: ExcelVBA@yahoogroups.com
Subject: [ExcelVBA] Using Parentheses in Subroutine Calls

The rule is that if you call a subroutine using the Call keyword, you
must enclose the argument list in parentheses. That is
Call Test(a, b)
is correct. But if you omit Call, you must not use parentheses, so that
Test a, b
is also correct. I didn't know these rules, and, from habit, I do not
use Call and do use parentheses. I got away with that for a long time
because my subroutines used only 1 argument. Surprisingly,
Test (c)
works even though it violates the rule. I got into trouble only when I
wrote a subroutine that used 2 arguments.
Test (c, d)
causes a syntax error. Does anyone know why the single argument works
with a parentheses?

[Non-text portions of this message have been removed]

----------

Visit our website at http://www.ubs.com

This message contains confidential information and is intended only
for the individual named. If you are not the named addressee you
should not disseminate, distribute or copy this e-mail. Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.

E-mails are not encrypted and cannot be guaranteed to be secure or
error-free as information could be intercepted, corrupted, lost,
destroyed, arrive late or incomplete, or contain viruses. The sender
therefore does not accept liability for any errors or omissions in the
contents of this message which arise as a result of e-mail transmission.
If verification is required please request a hard-copy version. This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities
or related financial instruments.

UBS reserves the right to retain all messages. Messages are protected
and accessed only in legally justified cases.

[Non-text portions of this message have been removed]

__._,_.___
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (2)
Recent Activity:
----------------------------------
Be sure to check out TechTrax Ezine for many, free Excel VBA articles! Go here: http://www.mousetrax.com/techtrax to enter the ezine, then search the ARCHIVES for EXCEL VBA.

----------------------------------
Visit our ExcelVBA group home page for more info and support files:
http://groups.yahoo.com/group/ExcelVBA

----------------------------------
More free tutorials and resources available at:
http://www.mousetrax.com

----------------------------------
.

__,_._,___

Tidak ada komentar:

Posting Komentar