+ Reply to Thread
Results 1 to 20 of 20

When Do Brackets Apply With Commands?

  1. #1
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    When Do Brackets Apply With Commands?

    I'm progressing with my VBA skills (still a complete amateur) but starting to get a small familiarity with common commands - I'll call them commands as I think functions is very ambiguous due to there being specific types of procedures that are 'Functions'.

    However I'm still struggling to grasp when brackets are used in the commands (not referring to subs & function parameters here) https://arstechnica.com/civis/viewtopic.php?t=662254. I also find the intellisence misleading. Am I the only one with this belief and could anyone shed some understanding?

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: When Do Brackets Apply With Commands?

    The answer is pretty much as in the link you posted. Broadly speaking, if you are using Call or if you are using the result of a function, then you use brackets; otherwise you do not.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,190

    Re: When Do Brackets Apply With Commands?

    Functions ALWAYS return a value so the params must be in parens. val = MyFunction(a,b,c)
    if you dont want the return value you can say: CALL MyFunction(....)

    Subroutines dont use the parens (since they dont return an answer)
    MySub a,b,c

    Brackets are used in places to embody the whole path, esp if it has spaces. Excel reads spaces as the end of the item. Brackets say its 1 item.
    [This Is my path.xls]

  4. #4
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: When Do Brackets Apply With Commands?

    Parens are used when a sub or function takes arguments (like a worksheet function).

    IE:
    When I do:
    Please Login or Register  to view this content.
    I can call it with:
    Please Login or Register  to view this content.
    But when I do:
    Please Login or Register  to view this content.
    then I must call it and pass it a string:
    Please Login or Register  to view this content.
    You also then have thing like byref and byval, etc.

    In what way do you find intellisense misleading? It basically gives you the possible methods, properties, arguments and in some cases objects/collections that can be used as you write your code. The only time I find it a problem is when it doesnt give me anything when I know there should be.
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: When Do Brackets Apply With Commands?

    Quote Originally Posted by ranman256 View Post
    Functions ALWAYS return a value so the params must be in parens. val = MyFunction(a,b,c)
    Only if you use it.
    Please Login or Register  to view this content.
    is also valid

  6. #6
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: When Do Brackets Apply With Commands?

    Thank you for your input.

    Remember guys I'm not talking in the context of Sub-Routines & Functions. I'm talking in the context of commands in Excel and trying to deduce from intellisence how you write out your code. As many have mentioned VBA is a rather unclear programming language where it's rationale is unclear.

    I wasn't able to ascertain much from my linked article. I saw different contrasting opinions. What I personally deduced was that when a value was passed this was usually placed in brackets.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: When Do Brackets Apply With Commands?

    What do you mean by commands, if not Subroutines and functions?

  8. #8
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: When Do Brackets Apply With Commands?

    Thanks Zero0Cool,

    Ok take a dictionary for e.g. (this is probably a terrible example as it's referencing another library):

    Please Login or Register  to view this content.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: When Do Brackets Apply With Commands?

    They're also used for things like this:
    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: When Do Brackets Apply With Commands?

    Quote Originally Posted by Dal123 View Post
    I wasn't able to ascertain much from my linked article.
    Thats because the article you linked is specifically in context of calls to subs and functions. If you mean how you use parens in a command the answer depends on what the command is composed of.

    Some are obvious, like using a VBA function. A VBA function like "Left()" always uses parens (as far as I know).

    However something like msgbox can be confusing to new programmers.

    Please Login or Register  to view this content.
    are both valid as is. To be honest I am not sure of the rules specifically for these as I typically just write the code and that syntax is second nature for me now.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: When Do Brackets Apply With Commands?

    You only use the := if you are using a named parameter- so
    Please Login or Register  to view this content.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: When Do Brackets Apply With Commands?

    When you invoke a Sub with Call, you must use parens around the arguments:

    Please Login or Register  to view this content.
    When you assign the result of a function to a variable, you must use parens around the arguments:

    Please Login or Register  to view this content.
    If you want to pass arguments ByVal (by value, a copy of the variable) or ByRef (by reference, a pointer to the variable), according to the procedure's signature, you don't include parens:

    Please Login or Register  to view this content.
    If you want to pass arguments ByVal, irrespective of the procedure’s signature (which won't work for a procedure expecting an array (because arrays are always passed by reference) or an object), you enclose the argument in parens, or an extra set of parens. In the examples below, arg1 is passed by value, and arg2 according to the procedure’s signature:

    Please Login or Register  to view this content.
    That’s because when you add parens around an argument that doesn't require them, the argument is evaluated, and the result of the evaluation is passed to the procedure. It means the argument is passed by value regardless of how the procedure requested it.

    That also explains why you can't use parens around two or more arguments when they are not required; the evaluation of (arg1) may be meaningful, but the evaluation of (arg1, arg2) is not. That's why this generates an error:

    Please Login or Register  to view this content.
    That also explains why if you call a procedure expecting a range with parens where none are required, you get a type mismatch error; because the evaluation of a range is a Variant, or Variant/Array.

    You can't pass a Variant ByRef as a static type (Long, String, ...). You can pass a statically-typed variable ByRef as a Variant, and the called procedure can modify its value with data of the appropriate (or coercible) type.

    So: Parens means completely different things according to context. VBA might have used some other bookends (curly braces, square brackets, whatever) to indicate enclosing a list of arguments versus a call for evaluation, but it didn't.

    Since objects are always passed ByRef, you can never put unnecessary parens around an object variable. Chip Pearson (http://www.cpearson.com/excel/byrefbyval.aspx) explains the meaning of ByRef and ByVal for an object variable:

    The ByRef and ByVal modifiers indicate how the reference is passed to the called procedure. When you pass an object type variable to a procedure, the reference or address to the object is passed -- you never really pass the object itself. When you pass an object ByRef, the reference is passed by reference and the called procedure can change the object to which that reference refers to. When an object is passed ByVal, a copy of the reference (address) of the object is passed.
    Entia non sunt multiplicanda sine necessitate

  13. #13
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: When Do Brackets Apply With Commands?

    Quote Originally Posted by xlnitwit View Post
    What do you mean by commands, if not Subroutines and functions?
    I mean the actual commands e.g. Range, Err.Number etc... Like I say I find it ambiguous to refer to the commands in excel as functions when using names for procedures like Function.

    I find it awkward (and many others by the dialogue of forum chat) determining the intentions of the intellisence commands.

  14. #14
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: When Do Brackets Apply With Commands?

    Quote Originally Posted by Zer0Cool View Post

    Please Login or Register  to view this content.
    are both valid as is.
    If you use the latter, you will note that the IDE actually amends it to
    Please Login or Register  to view this content.
    with a space after msgbox. In this context the brackets are evaluating the expression, so it is exactly the same as
    Please Login or Register  to view this content.
    However, if you tried to pass more than one parameter like that you would get an error
    Please Login or Register  to view this content.
    since it makes no sense to try and evaluate ("test", vbokonly). If you are not storing the result of the msgbox function, you should use
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    though the latter looks decidedly odd to my eyes.

  15. #15
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: When Do Brackets Apply With Commands?

    Quote Originally Posted by Dal123 View Post
    I mean the actual commands e.g. Range, Err.Number etc...
    Those are objects/properties, rather than commands.

  16. #16
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: When Do Brackets Apply With Commands?

    Quote Originally Posted by xlnitwit View Post
    You only use the := if you are using a named parameter- so
    Please Login or Register  to view this content.
    Thanks guys, some great input from you all here.

    I've read countless tutorials now (I'm talking about 280 hours of study as a guess). I'm well aware of all their meanings on paper, parameters, collections, objects etc....

    xlnitwit - take for example the parameters itself; we're adding parameters; you can't add := in both areas of the parameter. The intellisence appears suggesting an opening (bracket). Yet it doesn't take a (.

    e.g. 2 Look at Range("") where a bracket is suggested by the intellisence, and this time (not like the above) the bracket does actually need to be specified.

  17. #17
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: When Do Brackets Apply With Commands?

    Quote Originally Posted by shg View Post
    Minimized to save a superfluous quotation
    Thanks shg, this is very well explained and I'm learning a lot here, as with all of you.

  18. #18
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: When Do Brackets Apply With Commands?

    Quote Originally Posted by Dal123 View Post
    xlnitwit - take for example the parameters itself; we're adding parameters; you can't add := in both areas of the parameter. The intellisence appears suggesting an opening (bracket). Yet it doesn't take a (.
    I’m afraid I don’t follow. Can you clarify what you mean with an example?

  19. #19
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: When Do Brackets Apply With Commands?

    Sorry for being unclear xlnitwit.

    Take for e.g.
    Please Login or Register  to view this content.
    Once you've entered dict.Add intellisence arrives and suggests a (. Untitled-1A.jpg We see this exact same thing from intellisence when we type Range(, where more accurately the intellisence is correct and a ( is required in the command
    Please Login or Register  to view this content.
    Note the exact same suggestion of brackets; but it is not required in the below.
    Please Login or Register  to view this content.

    Now I know from the great input that I've just had from you guys that has really helped me grasp that we're not actually returning a value so it's not required here; but look at how misleading intellisence is.

  20. #20
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: When Do Brackets Apply With Commands?

    Intellisense isn’t contextual in that way. It doesn’t examine your code to see if you are using Call or returning a value, it just gives the basic syntax. Bear in mind the IDE is over 20 years old.
    Last edited by xlnitwit; 03-06-2018 at 04:21 PM. Reason: Fix autocorrect

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Summing brackets, when you have multiple brackets per cell
    By tmiller1002 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-26-2017, 11:22 AM
  2. [SOLVED] Apply VBA Script based on Text in one column and apply a formula in another column
    By vvincent in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-07-2016, 09:19 PM
  3. [SOLVED] Automating Brackets, and Sum above Brackets in Excel Chart
    By cartica in forum Excel Charting & Pivots
    Replies: 10
    Last Post: 07-24-2014, 11:33 AM
  4. [SOLVED] Apply Macro First UnprotectSheet After Apply Macro ProtectAgain Solved by : Sixthsense
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2014, 05:32 AM
  5. Macro Recording Chart Format commands missing all important commands!!!!
    By nounours in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-05-2013, 09:20 PM
  6. Replies: 5
    Last Post: 04-19-2012, 09:02 AM
  7. {} Brackets
    By Directlinq in forum Excel General
    Replies: 3
    Last Post: 04-28-2009, 04:08 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1