Results 1 to 5 of 5

Declaring Variables

This is a discussion on Declaring Variables within the Excel Questions forums, part of the Question Forums category; Could someone please explain to me the benefits or differences in declaring variables the following two ways: --------------------------------- Dim Variable, ...

  1. #1
    Board Regular
    Join Date
    Apr 2002
    Posts
    85

    Default

    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. #2
    dk
    dk is offline
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    2,932

    Default

    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

  3. #3
    MrExcel MVP Russell Hauf's Avatar
    Join Date
    Feb 2002
    Location
    Beaverton, OR USA
    Posts
    1,401

    Default

    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).

  4. #4
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530

    Default

    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. #5
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530

    Default

    **** these slow fingers.

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com