In Const statement, 5.0 --> 5#?

H

Heather Mills

While writing a word macro, I typed the statement

Const PageWid = 5.0

As soon as I pressed Enter, it switched to

Const PageWid = 5#

Why is that?
 
J

Jay Freedman

While writing a word macro, I typed the statement
Const PageWid = 5.0

As soon as I pressed Enter, it switched to

Const PageWid = 5#

Why is that?

When you enter a number that includes a decimal point followed only by one or more zeros, VBA automatically replaces that number with the equivalent integer followed by a # sign, which is the "type
declaration character" for the Double type.

If you want a different data type, the other numeric type declaration characters are an exclamation point (!) for the Single type and a percent sign (%) for the Integer type.
 
H

Heather Mills

When you enter a number that includes a decimal point followed only by one or more zeros, VBA automatically replaces that number with the equivalent integer followed by a # sign, which is the "type
declaration character" for the Double type.

OK, thanks. Weird syntax.
If you want a different data type, the other numeric type declaration characters are an exclamation point (!) for the Single type and a percent sign (%) for the Integer type.

I'll just leave it as

Const PageWid = 5

VBA doesn't mess with that.

I even tried explicitly declaring the type

Const PageWid as Variant = 5.00
Const PageWid as Double = 5.00

VBA still insists on converting the "5.00" to "5#". It's very annoying
(and a little arrogant) that VBA won't let me code like I want to.

Anyway, thanks for the explanation.
 
T

Tony Jollans

VBA always removes redundant zeroes from numeric literals - and adds, or removes, type descriptors when it deems it appropriate. It is, however, taking note of the fact that you have entered a decimal point and is explicitly declaring the constant as a Double, rather than the default Long.

There is a difference between:

Const PageWid = 5# ' explicitly Double

and

Const PageWid = 5 ' default Long

Although the two have the same value, they trigger different behaviour in code.

Look at the result of these two pieces of code to see one example of it:

Const a = 5#
Dim b As Long, c
b = 2 ^ 30: c = 0
On Error Resume Next
c = a * b
MsgBox c


Const a = 5
Dim b As Long, c
b = 2 ^ 30: c = 0
On Error Resume Next
c = a * b
MsgBox c


These are artificial examples, but do show a difference that might be significant in some circumstances.
 
H

Heather Mills

VBA always removes redundant zeroes from numeric literals - and adds, or removes, type descriptors when it deems it appropriate. It is, however, taking note of the fact that you have entered a decimal point and is explicitly declaring the constant as a Double, rather than the default Long.

There is a difference between:

Const PageWid = 5# ' explicitly Double

and

Const PageWid = 5 ' default Long

Although the two have the same value, they trigger different behaviour in code.

Look at the result of these two pieces of code to see one example of it:

Const a = 5#
Dim b As Long, c
b = 2 ^ 30: c = 0
On Error Resume Next
c = a * b
MsgBox c


Const a = 5
Dim b As Long, c
b = 2 ^ 30: c = 0
On Error Resume Next
c = a * b
MsgBox c


These are artificial examples, but do show a difference that might be significant in some circumstances.

Sure, I have no problem with that, but if I explicitly code the type,
VBA really ought to leave it the way I type it unless there's an
error. I have a visceral reaction when a piece of software thinks it
knows what's best for me. ;-)
 
T

Tony Jollans

Another way of looking at it ...

Every language has its syntax and, here, 5.0 is not valid.

What the editor is saying is that you have entered invalid syntax but, because it is pretty certain it knows what you mean it will correct things for you, rather than bother you with an error message


In a sense it does know what's best for you, because you cannot seriously want invalid code.
 
H

Heather Mills

Another way of looking at it ...

Every language has its syntax and, here, 5.0 is not valid.

What the editor is saying is that you have entered invalid syntax but, because it is pretty certain it knows what you mean it will correct things for you, rather than bother you with an error message


In a sense it does know what's best for you, because you cannot seriously want invalid code.

Not to beat a dead horse, but how is this syntax invalid?

Const xyz as Double = 5.00

or even this?

Const xyz = 5.00
 
T

Tony Jollans

Invalid .. because the syntax does not allow a numeric literal to include insignificant zeroes anywhere in VBA.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top