Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By anvil19

VBA - Sub with parameters

This is a discussion on VBA - Sub with parameters within the Excel Questions forums, part of the Question Forums category; Hi, could you tell what is the difference in calling sub with parameters? Example: Sub ABC(Par1,...) So what is the ...

  1. #1
    Board Regular
    Join Date
    Jan 2004
    Location
    Silesia
    Posts
    98

    Default VBA - Sub with parameters

    Hi,
    could you tell what is the difference in calling sub with parameters?
    Example:
    Sub ABC(Par1,...)
    So what is the difference between:
    1) Call ABC (X, ...)
    2) Call ABC X
    3) Call ABC Par1:=X
    Thank you guys

  2. #2
    Board Regular
    Join Date
    Nov 2003
    Posts
    230

    Default Re: VBA - Sub with parameters

    Hey bruno.trudo


    This information is taken straight from the Visual Basic Help:

    A Sub procedure differs from a Function procedure in that a Sub procedure cannot be called by using its name within an expression. A call to a Sub is a stand-alone statement. Also, a Sub does not return a value in its name as does a function. However, like a Function, a Sub can modify the values of any variables passed to it.

    There are two ways to call a Sub procedure:

    Code:
    ' Both of these statements call a Sub named MyProc.
    Call MyProc (FirstArgument, SecondArgument)
    MyProc FirstArgument, SecondArgument
    Note that when you use the Call syntax, arguments must be enclosed in parentheses. If you omit the Call keyword, you must also omit the parentheses around the arguments

    Hope that helps

    anvil19
    :o
    James Snyder likes this.

  3. #3
    Board Regular
    Join Date
    Jan 2004
    Location
    Silesia
    Posts
    98

    Default Re: VBA - Sub with parameters

    Thank you anvil19,
    but what about the third one. Using :=
    Do you know?

  4. #4
    Board Regular
    Join Date
    Nov 2003
    Posts
    230

    Default Re: VBA - Sub with parameters

    Hi again

    I found this in the VB help that could be of use

    Creating Simpler Statements with Named Arguments

    For many built-in functions, statements, and methods, Visual Basic provides the option of using named arguments as a shortcut for typing argument values. With named arguments, you can provide any or all of the arguments, in any order, by assigning a value to the named argument. You do this by typing the argument name plus a colon followed by an equal sign and the value ( MyArgument:= "SomeValue") and placing that assignment in any sequence delimited by commas. Notice that the arguments in the following example are in the reverse order of the expected arguments:

    Function ListText(strName As String, Optional strAddress As String)
    List1.AddItem strName
    List2.AddItem strAddress
    End Sub

    Private Sub Command1_Click()
    ListText strAddress:=12345, strName:="Your Name"
    End Sub

    This is especially useful if your procedures have several optional arguments that you do not always need to specify.

    Determining Support for Named Arguments
    To determine which functions, statements, and methods support named arguments, use the AutoQuickInfo feature in the Code window, check the Object Browser, or see the Language Reference. Consider the following when working with named arguments:

    Named arguments are not supported by methods on objects in the Visual Basic (VB) object library. They are supported by all language keywords in the Visual Basic for applications (VBA) object library.


    In syntax, named arguments are shown as bold and italic. All other arguments are shown in italic only.
    Important You cannot use named arguments to avoid entering required arguments. You can omit only the optional arguments. For Visual Basic (VB) and Visual Basic for applications (VBA) object libraries, the Object Browser encloses optional arguments with square brackets [ ].
    anvil19
    :o

  5. #5
    Board Regular
    Join Date
    Jan 2004
    Location
    Silesia
    Posts
    98

    Default Re: VBA - Sub with parameters

    Oh thanks,
    you are really dab at help seeking.
    Have a nice day!
    bruno

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com