![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Apr 2002
Posts: 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! |
|
|
|
|
|
#2 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
|
Quote:
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 |
|
|
|
|
|
|
#3 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
|
Quote:
-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). |
|
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
|
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 |
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
|
Damn these slow fingers.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|