×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Optional argument in subroutine not functioning according to MS

Optional argument in subroutine not functioning according to MS

Optional argument in subroutine not functioning according to MS

(OP)
Folks,

I tried using an optional argument in a subroutine. When I try calling the subroutine from another subroutine and attempt to enter the second (optional) argument, the line of code turns red. When I try compiling I get a syntax error.
I'm attaching a Word .docx file showing the subroutine with the optional argument and what happens when I try to call it.

This is very confusing.

Any thoughts?

Vic

RE: Optional argument in subroutine not functioning according to MS

Either:

CODE

Call SnglFilter("9", 1) 
or

CODE

SnglFilter "9", 1 

BTW - cmd9 is not very descriptive name for a command button....


---- Andy

There is a great need for a sarcasm font.

RE: Optional argument in subroutine not functioning according to MS

(OP)
Thanks Andy I'll try those.
The button is the number 9
Vic

RE: Optional argument in subroutine not functioning according to MS

>The button is the number 9

heh!

RE: Optional argument in subroutine not functioning according to MS

(OP)
Andy,

Thanks for the tip. That works.

But I still think it's confusing to split hairs like that. If you use the Call, you put the arguments in parens. Without the Call you don't use parens.

I don't think I ever knew that level of granularity. LOL

Thanks again,

Vic

RE: Optional argument in subroutine not functioning according to MS

I always use Call MySub(parameter, another) and never have a problem. Plus I know if I have a word Call - I step into another piece of code.

>I still think it's confusing
Well, complain to Microsoft laughtears


---- Andy

There is a great need for a sarcasm font.

RE: Optional argument in subroutine not functioning according to MS

>confusing to split hairs like that

It's fairly explicit in the VBA documentation:

Quote:

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.

RE: Optional argument in subroutine not functioning according to MS

(OP)
Thanks strongm.

I hadn't actually seen that documentation.

There is still some confusion. When I didn't use the Call keyword, the line of code was SnglFilter ("9"). According to the documentation I shouldn't have the parens. However, this code compiled and did not produce a runtime error. So even though the documentation says to omit the parens when not using the Call keyword, apparently that doesn't apply when using only one argument. I only got the error when I included the second (optional) argument without the Call keyword.

RE: Optional argument in subroutine not functioning according to MS

>apparently that doesn't apply when using only one argument
That's not what is happening, as the parenthesis in this case is not functionally part of the procedure call (note the space between the procedure name and the opening bracket that the IDE will keep insisting on putting in if the expression within the parenthesis can be evaluated)

Consider the following, and see if you can see what is happening:

CODE

Public Sub test()
    Dim fred As Long
    
    fred = 0
    example fred
    MsgBox fred
    fred = 0
    example (fred)
    MsgBox fred
    
End Sub

Public Sub example(ByRef temp As Long)
    temp = 5
End Sub 

RE: Optional argument in subroutine not functioning according to MS

(OP)
strongm

I'm trying to understand this.

Are you saying that if the argument in parens can be evaluated, that it's essentially being passed ByVal and not ByRef?
It's my understanding that if you don't include the ByRef keyword, it's assumed because it's the default.

Vic

RE: Optional argument in subroutine not functioning according to MS

>that it's essentially being passed ByVal and not ByRef?

No.

I'm trying to show that with

SomeName (x)

(x) is NOT an "argument in parens", it is a simply expression. The expression is evaluated before being passed as parameter (which was the point of my example). Essentially it is a shorthand for:

SomeName Evaluate(x)

effectively, it is handled as:

y = Evaluate(x)
SomeName y

which happily meets the rules

(Whether the expression is evaluable is checked at design time which is why you get an error if you try and add a second 'parameter')



RE: Optional argument in subroutine not functioning according to MS

(OP)
strongm

Ok. I think I understand that now.

Thanks for you patience.

Vic

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close