"!" at end of variable

  • Thread starter Thread starter joak
  • Start date Start date
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
 
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)
 
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.
 
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
 
Chip, JE, and Bob-
Ah, I get it now. Thank you very much! You guys are
great!

Joak
 
# As Long

This should, of course, be As Double.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
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.
 

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

Back
Top