none
VBA (Excel) question RRS feed

  • Question

  • Hello,

    I am new to VBA...can someone please elaborate what the following line is doing?  I know it's initializing a variable named rng1 but is it of type "range" and if yes, how many values can this range store?  

    Dim rng1(1 To 1, 1 To 1)

    Furthermore, can I do something similar in VBScript?  If yes, how?

    Thanks for help!

    Wednesday, November 16, 2016 12:58 AM

All replies

  • The (1 to 1, 1 to 1) means that the variable is an array with 1 row and 1 column, i.e. an array with just 1 item: rng1(1, 1).

    Since the data type of the variable is not specified, it is an array of type Variant. This means that any kind of value (text, number, object) can be stored in the single array element.

    In VBScript, you cannot use ... To ... when declaring an array. You can only specify the upper bounds; the lower bounds are always 0. So the line

    Dim rng1(1, 1)

    would be valid in VBScript; it is the equivalent of

    Dim rng1(0 To 1, 0 To 1)

    which has 4 elements: Rng1(0, 0), Rng1(1, 0), Rng1(0, 1) and Rng1(1, 1).

    If you want to declare a single-element array in VBScript, use

    Dim rng1(0, 0)

    This has just the single element rng1(0, 0).


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Wednesday, November 16, 2016 7:10 AM
  • Thank you for the answer.
    Wednesday, November 23, 2016 7:03 PM
  • Hi,

    If your issue has been resolved, I suggest you mark helpful post as answer.

    If you have any new issues, please feel free to post threads. Many community members and we are willing to help.

    Thanks for your understanding.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 24, 2016 12:15 PM
    Moderator