In VBA a declaration cannot legally be joined with an assignment in the same instruction, so that would be:
Dim myDate As Date
myDate = #1/1/2021#
For the sake of this discussion it's important to differentiate the variable from the literal value, because the declared type of the variable is irrelevant to the type of the literal - this is perfectly confusing, but perfectly legal too:
Dim myDate As Long
myDate = #1/1/2021#
We're talking about the literals here.
A numeric literal is an Integer
literal if it's between -32767 and 32767, a Long
literal if it's between -2147483647 and 2147483647, and a Double
otherwise (the VBE automatically adds a #
type hint then).
That means the 200
in myAmount = 200
is an Integer
literal, and if we typed 200.00
the VBE would "prettify" it to 200#
(making it a Double
literal).
The type hint character to use for a Currency
literal, is @
, so the 200
in myAmount = 200@
is a Currency
value that the VBA compiler understands as such.
It doesn't matter though, because VBA will do lots of widening (narrowing too) implicit type conversions for us: if we assign an Integer
literal to a Currency
variable, the Integer
value is converted to "fit" the reserved Currency
"bucket". So if we declared myAmount As Currency
and then assigned it to the literal value 200
(an Integer
literal), if we asked VBA what type myAmount
is, we would get the declared type - Currency
.
In other words it's perfectly legal to assign an Integer
or a Double
literal to a Currency
variable, and that makes type-hinted literals somewhat awkward beasts that don't really belong, in the vast majority of situations:
Dim myAmount As Currency
myAmount = 200 'integer literal
Debug.Print TypeName(myAmount) 'currency
myAmount = 200# 'double literal
Debug.Print TypeName(myAmount) 'currency
myAmount = 200@ 'currency literal
Debug.Print TypeName(myAmount) 'currency
Things get groovy when we drop the declared type and use a Variant
instead (implicit or not):
Dim myAmount 'As Variant
myAmount = 200 'integer literal
Debug.Print TypeName(myAmount) 'integer
myAmount = 200# 'double literal
Debug.Print TypeName(myAmount) 'double
myAmount = 200@ 'currency literal
Debug.Print TypeName(myAmount) 'currency
A date literal needs #
"delimiters" on both sides, because the /
date separator character has another grammatical meaning in VBA (and many other languages too): in other contexts, it's the division operator: in a sense, #
delimiters are more closely related to the "
double quotes that delimit string literals, than they are to @
type hints.