What are the suffixes after a variable name in VBA?

As I already found out, there are at least six of them: !@ #$%& .

Here is a snip:

  Dim A !, B@ , C #, D $, E%, F &
 Debug.Print "A! -" & TypeName (A)
 Debug.Print " B@ -" & TypeName (B)
 Debug.Print "C # -" & TypeName (C)
 Debug.Print "D $ -" & TypeName (D)
 Debug.Print "E% -" & TypeName (E)
 Debug.Print "F & -" & TypeName (F)

Outputs

  A!  - Single
 B@ - Currency
 C # - Double
 D $ - String
 E% - Integer
 F & - Long

Where is the documentation for this syntactic sugar located?

What other suffixes are there?

Is it for a date?

+8
vba
source share
3 answers

A complete (?) List is here http://support.microsoft.com/kb/110264 under Prefixes of Variable and Function Names. As Remu said, they are not recommended (the article says that the use is "not recommended"). I believe that you have included them in your list. The date is technically an option (stored as a floating point number), so there is no shortcut for it.

+4
source share

These suffixes are type hints, and the link in the accepted answer is out of date.

 Dim someInteger% '% Equivalent to "As Integer" Dim someLong& '& Equivalent to "As Long" Dim someDecimal@ '@ Equivalent to "As Currency" Dim someSingle! '! Equivalent to "As Single" Dim someDouble# '# Equivalent to "As Double" Dim someString$ '$ Equivalent to "As String" Dim someLongLong^ '^ Equivalent to "As LongLong" in 64-bit VBA hosts 

So, you had all of them except ^ for LongLong introduced in VBA7 for 64-bit host applications. That is why Microsoft introduced a new type of tooltip for a new type of value, but I don't know.

This is more syntactic poison than syntactic sugar, although it dates all the way from the ancestors, the dinosaur versions of BASIC before the As sentence was something like, for example, in this 64 BASIC 2.0 Commodore fizzbuzz code:

 1000 REM INIT VARIABLES 1010 LET FIZZ$ = "FIZZ" 1011 LET BUZZ$ = "BUZZ" 1020 LET FIZZ% = 3 1021 LET BUZZ% = 5 1030 LET MIN% = 1 1031 LET MAX% = 15 1100 PRINT FIZZ$ + ":" + STR$(FIZZ%) 1101 PRINT BUZZ$ + ":" + STR$(BUZZ%) 1102 PRINT FIZZ$ + BUZZ$ + ":" + STR$(FIZZ%*BUZZ%) 1105 PRINT 

As you can see, type hints are not the only paleocode supported by VBA: line numbers, Rem comments, and explicit expressions of the Let value were also a subject in 1982. Avoid them at all costs.

Literals prefer explicit conversions over types:

 Debug.Print TypeName(32&) 'prints Long Debug.Print TypeName(CLng(32)) 'prints Long 

Ask yourself if you can ask yourself . - unknown

+5
source share

They are outdated and remain only for backward compatibility AFAIK. Declare your variables as required types. You can also force variables to type.

+3
source share

All Articles