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?
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?
Yes, but you lose the decimals.1) Can Dim x as Long handle decimal?
[VBA]
x=123.45
[/VBA]
Returns 123.
No, not necessary but highly recommended, including using Option Explicit at the top of all modules.2) Is it necessary to DIM all variables in a VBA program? Can I just declare a few but leave some out?
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
Thanks a lot. So what should I do If I want to preserve the decimal? I guess I should use Double, right?
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
Got it, hot shot. Thanks
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]
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
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]
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