Results 1 to 3 of 3
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Why $? (Word VBA)

    Hi all, I'm working on some pre-coded templates and in several places I've found code words with a $ sign on the end where I would have used it in exactly the same way but without the $ - eg Format$ instead of Format, Trim$ instead of Trim, etc.

    What does this do, please? And should it be used in preference to the plain word? I've checked the 'help' but there doesn't appear to be anything there on the subject.

    Many thanks!
    Beryl M


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Why $? (Word VBA)

    Many string functions have two versions, one that returns a Variant and another that returns a String.

    For example, Trim returns a Variant, while Trim$ returns a String.

    In theory, Trim$ is more efficient than Trim, because VBA doesn't have to convert the Variant result to a String for further processing, it has already been done. But in practice, you won't notice the difference.

    The two versions differ in the way they handle Null (blank, empty) values. If you pass a variable or expression that evaluates to Null as argument to Trim, it will return Null. But if you pass it to Trim$, you'll get an error message "Invalid use of Null".

    Ten or fifteen years ago, it was often recommended to use the $ versions where possible, because they make the code run more efficiently. But with today's computers, that doesn't matter any more, and since the versions without $ are more flexible (as mentioned above, they handle Nulls without causing an error), I'd use the latter.

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Why $? (Word VBA)

    Many thanks, Hans - just what I needed to know!

    <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Beryl M


Posting Permissions

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