Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring variables in VBA

Tags:

vba

Dim x, y as Date

What is the difference between these two code statements

Dim x as Date, y as Date

What is the pragmatic result of the difference and what other hidden declaration features am I missing?

like image 986
CodeCamper Avatar asked May 07 '26 11:05

CodeCamper


2 Answers

Dim x, y as Date

is the equivalent of:

Dim x 
Dim y as Date

and that is the equivalent of:

Dim x as Variant
Dim y as Date

When you omit the type for a variable, it assumes the Variant type

Edit, per your question:

Dim a! ' same as Dim a as Short
Dim b@ ' same as Dim b as Currency
Dim c# ' same as Dim c as Double
Dim d$ ' same as Dim d as String
Dim e% ' same as Dim e as Integer
Dim f& ' same as Dim f as Long

ref: http://bytes.com/topic/visual-basic/answers/643371-declaration-shortcuts

like image 111
Vinicius Kamakura Avatar answered May 11 '26 16:05

Vinicius Kamakura


Dim x, y as Date

As <Type> is required for each variable, so in the example above only y is a Date; x is a Variant (The default when no type is specified)

Dim x as Date, y as Date

Both variables are Dates as each has an As.

like image 26
Alex K. Avatar answered May 11 '26 14:05

Alex K.