Consulting

Results 1 to 6 of 6

Thread: Solved: How do you Redim Preserve a 2 dimensional array?

  1. #1
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location

    Question Solved: How do you Redim Preserve a 2 dimensional array?

    Hey everyone,

    This is annoying, and I can't find this on the web...or in help files. Not sure if I'm not looking in the right places. But could someone help me with this code?

    [vba]Sub test_Array()
    Dim varArray() As Variant
    Dim i As Integer

    For i = 0 To 9
    'ReDim Preserve varArray(UBound(varArray, 1) + 1, 1)
    'ReDim Preserve varArray(i, 1)

    varArray(i, 0) = i
    varArray(i, 1) = i * i
    Next
    End Sub[/vba]
    I keep getting a "Subscript out of range" error with the two commented out lines (just two methods I tried).

    Any suggestions?




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hey Joseph,

    Try this as an example. You can tweak to suit...
    [vba]Sub test_Array()
    Dim varArray() As Variant
    Dim i As Integer
    Dim lrows As Long
    Dim lcols As Long

    lrows = 5
    lcols = 10

    ReDim Preserve varArray(0 To lcols, 0 To lrows)

    For i = 0 To 9
    varArray(i, 0) = i
    varArray(i, 1) = i * i
    Next
    End Sub[/vba]
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    Quote Originally Posted by malik641
    Hey everyone,

    This is annoying, and I can't find this on the web...or in help files. Not sure if I'm not looking in the right places. But could someone help me with this code?

    [vba]Sub test_Array()
    Dim varArray() As Variant
    Dim i As Integer

    For i = 0 To 9
    'ReDim Preserve varArray(UBound(varArray, 1) + 1, 1)
    'ReDim Preserve varArray(i, 1)

    varArray(i, 0) = i
    varArray(i, 1) = i * i
    Next
    End Sub[/vba]
    I keep getting a "Subscript out of range" error with the two commented out lines (just two methods I tried).

    Any suggestions?
    From help;
    If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.

    You are Redim the 1st which is not allowed.
    Kind Regards,
    Ivan F Moala From the City of Sails

  4. #4
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Awesome! Thanks Ken and Ivan!!!!!

    I combined your two suggestions to give me what I need.

    Here comes another KB baby!!! Oh yeah!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Joseph,
    You could also consider using, say a 2 x 10 array, resize the 10 as required, and finally transpose to a 10 x 2 array for placing your data in a spreadsheet or whatever. There is a fairly low limit 5,400 items you can transpose in older versions of Excel, but I don't know what the limit is in 2003.
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Malcom,

    The i = 0 To 9 was just a small example. The thing was, that I don't know what the second dimension of the array will be (in my actual code), so that's why I can't define it in the beginning.

    I don't have a reason to use the transpose in this case, but I never thought about doing that. Sounds useful, thanks




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

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