Declaring Variables

waderw

Board Regular
Joined
Apr 26, 2002
Messages
85
Could someone please explain to me the benefits or differences in declaring variables the following two ways:
---------------------------------
Dim Variable, Variable2 as String

and

Dim Variable3 as String, Variable4 as String
----------------------------------
I know that the first declaration statement declares Variable as a String and Variable2 as a variant.

The second declaration statement declares both Variable3 and Variable4 as Strings.

But what are the differences and advantages/disadvantages of using the first declaration statement as compared to the second?

Thanks!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
On 2002-05-23 08:17, waderw wrote:
Could someone please explain to me the benefits or differences in declaring variables the following two ways:
---------------------------------
Dim Variable, Variable2 as String

and

Dim Variable3 as String, Variable4 as String
----------------------------------
I know that the first declaration statement declares Variable as a String and Variable2 as a variant.

The second declaration statement declares both Variable3 and Variable4 as Strings.

But what are the differences and advantages/disadvantages of using the first declaration statement as compared to the second?

Thanks!

Hi,

Your first example dimensions Variable as variant and Variable2 as string. If you Variable is going to be used for a string then you should declare it as such. If you fail to declare a variable, declare it as Variant or don't specify a data type then VBA will create a Variant variable. These will take up more space in memory than a String and will produce slower results if you're going to be manipulating the contents of the variable.

Regards,
Dan
 
Upvote 0
On 2002-05-23 08:17, waderw wrote:
Could someone please explain to me the benefits or differences in declaring variables the following two ways:
---------------------------------
Dim Variable, Variable2 as String

and

Dim Variable3 as String, Variable4 as String
----------------------------------
I know that the first declaration statement declares Variable as a String and Variable2 as a variant.

The second declaration statement declares both Variable3 and Variable4 as Strings.

But what are the differences and advantages/disadvantages of using the first declaration statement as compared to the second?

Thanks!

Actually, you had the first example backwards (Variable would be Variant, and Variable2 would be String). The advantages of declaring variables (explicitly, as String, Integer, etc.) are:

-Space: Variants take more memory
-Avoiding possible errors: in some cases, if you try to assign something to a variable that isn't the same type, you'll get an error (which is a good thing - if you have an Integer variable, you don't want someone assigning "Hello" to it).
 
Upvote 0
Actually, the first declaration declares "Variable" as a variant and "Variable2" as a string.

There is absolutely no advantage to the first syntax other than to confuse the programmer. With a cursory glance, the programmer may think that both of these variables are string types. There are also memory issues and processing overhead to consider when using variant types.

Go to VBA help and search for "understanding Variants" to get a decent description of the disadvantages of using variant type over explicit type variables.

HTH
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top