"!" at end of variable

J

joak

Hi-

I have the pleasure of fixing another's vba code and,
having very little experience with vba myself, I had a
question about variable names.

From what I've read, vba variables shouldn't have !, &,
%, etc. in the name. However, in this particular piece
of code, there are some Dim statements declaring some
variables with an "!" at the end. In the actual code,
however, the "!" is left out. What is this all about?
TIA!

joak
 
B

Bob Phillips

Joak,

Declaring a variable in this way is typing it at the same time.

Dim a! is the same as Dim a As Single
Dim a% is the same as Dim a As Integer.

The variable though is just called a, so that is all you need to refer to it
by in the code.

Personally, I think it is ugly syntax, much better to declare the type
clearly.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
J

JE McGimpsey

It's not part of the variable name, per se. It's a type declaration
character. ! indicates a Single, so

Dim foo!

is equivalent to

Dim foo As Single

Each data type (other than Variant and Object) has a type declaration
character. You can see them in XL/VBA Help.
 
C

Chip Pearson

Joak,

The '!' at the end of the variable name forces that variable to
be of type Single. The '!' is a substitute for "As Single".
Other type specification characters include

$ As String
# As Long
@ As Currency
% As Integer
& As Long


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
J

JE McGimpsey

Bob Phillips said:
Personally, I think it is ugly syntax, much better to declare the type
clearly.

That, and whoever decided that the *percent* sign should indicate an
*integer* should have been shot.
 
Top