Consulting

Results 1 to 9 of 9

Thread: Two questions: Long and Dim

  1. #1

    Two questions: Long and Dim

    1) Can Dim x as Long handle decimal?


    2) Is it necessary to DIM all variables in a VBA program? Can I just declare a few but leave some out?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1) Can Dim x as Long handle decimal?
    Yes, but you lose the decimals.

    [VBA]
    x=123.45
    [/VBA]

    Returns 123.


    2) Is it necessary to DIM all variables in a VBA program? Can I just declare a few but leave some out?
    No, not necessary but highly recommended, including using Option Explicit at the top of all modules.

    OE forces all variables to be Dim-ed, and decreases problems caused my mis-typing

    If you don't Dim your variables, VBA makes them all Variant, which also can make debugging more difficult, as well as introducing small delays when VBA has to convert types

    My 2 cents ...

    Paul

  3. #3
    Thanks a lot. So what should I do If I want to preserve the decimal? I guess I should use Double, right?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Right, as we said yesterday.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Got it, hot shot. Thanks

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could Dim the decimal variable as Single instead of Double. Singles support larger values, Doubles greater precision. It's your choice.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is wasteful to define single (or integer) as it forces two conversions of data, to double (long) when taken by the processing engine, and back to single (integer) when returned.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi James,

    If you're working with decimals, why would a conversion to/from double be needed? And how would that be possible for values larger than a double can handle?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because that it what 32/64 bit systems do.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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