+ Reply to Thread
Results 1 to 6 of 6

Different Syntax’s

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Different Syntax’s

    I am wondering why some syntax’s are different than others in regards to vba functions and if there is a difference between types, which one is faster,
    Which is better and which is the best coding practise to use)

    As an example I will use this code:
    .Replace What:="cat", Replacement:="dog"
    Why can I not write:
    .Replace(“cat”, “dog”)
    There appears to be some codes that use the syntax:
    .Function(variable1, variable2,[variable3])
    And others that use this syntax
    .Function variable1:=a, variable2:=b, variable3:=c
    I prefer to use the first but some functions only allow one way... can anyone explain this?

    Thank you

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    Quote Originally Posted by penfold1992:3173673
    Why can I not write:
    .Replace(“cat”, “dog”)
    Eh, you can use that.

    You would need to remove the brackets though and use proper quotes, not sure where they came from.
    If posting code please use code tags, see here.

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Different Syntax’s

    your problem is simply the parentheses (and strange quote marks)-you can write either
    .Replace "cat", "dog"
    or
    .Replace What:="cat", Replacement:="dog"
    with any function in vba you can pass arguments by name or position. if there are a lot of optional arguments before the one you want to use, named arguments are simpler as well as being much easier to read and maintain
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Forum Contributor
    Join Date
    03-27-2012
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: Different Syntax’s

    why do some have parenthesis and some dont? (the strange quote marks was because my gf posted the message because i cant post at work, well now I can... argh long story dont worry XD )

    Range("A1").
    Sheets("Sheet1").
    Format(Month, "00")
    IfDate(Now)

    these all have parenthesis yet there are also some that dont? what about these examples:
    Range("A1").HorizontalAlignment = xlCenter
    Range("A1").Font.Size = 14

    why do they have "=" , cant they just be:
    .Font.Size(14)

    is one faster then another? (in terms of how much memory it requires)

    The reason I am asking all these questions is because I wish to persue a career in programming and I want to make sure I get into good practises even at the low level I am at currently (I dont exactly know where VB is in terms of difficulty/usefullness of programming language but ive found it a good start)

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Different Syntax’s

    I honestly wouldn't worry about syntax affecting the speed of code or the amount of memory used.

    Syntax is more likely to stop the code working.

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Different Syntax’s

    the first ones are methods/functions and take arguments, the last ones are properties to which you assign a value. there is no performance difference between the two that I am aware of

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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