Consulting

Results 1 to 5 of 5

Thread: Variable declaration

  1. #1

    Variable declaration

    Hi all,

    I'm trying to "read" the code of a colleague and I'm stock in a thing.

    He declared a variable like you can see below and I can not figure out the scope of it.

    Maybe you can help me.



    Dim variable(100, 1) as String


    I do not know what do the two values between parenthesis.

    Thank you in advance!

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    It's a String array of 101 rows and 2 columns (unless there's an Option Base 1 statement at the top of the module).
    Be as you wish to seem

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Fully expanded it's

    Dim VariableName (0 to 100, 0 To 1) As String
    for a total of 202 Strings (101 x 2)

    Unless as Aflatoon says there's an

    Option Base 1
    at the top (outside of subs) of the module, it's assumed as Option Base 0, so any unspecified lower limits are assumed to start at 0
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Hi,

    Thanks a lot to both of you.

    I have not known that Dim variable_name(0 to 100, 0 to 1) as String can be written Dim variable_name(100, 1) as String.

    To be honest I've used more one dimension arrays than two dimension ones, that's why my childish question and now if I remember correctly, in case of one dimension array, Dim variable(0 to 10) as String is the same with Dim variable(10) as String so I could have done the analogy if I was smart enough.

    Regarding Option Base 1, is was not the case in my example but thanks for reminding the importance of it.

    All the best!

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Regarding Option Base 1, is was not the case in my example but thanks for reminding the importance of it.
    I don't use it myself, but sometimes I'll inherit a macro where it is used. I always use LBound(…) even if I 'know' it's Base 0

    As a second reminder, the (0 To N) is not written is stone

    Sometimes I need to (say) move rows 20 to 29 into an array

    Dim Ary (20 To 29) can simplify the macro and make it easier to read

    For I = LBound(Ary) to UBound(Ary)
        Ary(I) = Cells(I, 1).value
    Next I


    if I was smart enough.
    No, that's called experience .. just keep on VBA-ing
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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