Consulting

Results 1 to 2 of 2

Thread: Solved: Redim Preserve

  1. #1

    Solved: Redim Preserve

    I am attemping to create a multi array of unknown size, which holds an id and a date within a loop. The following cose gives me a 'subscript outn of range' error. Any suggestions?

    thank you


    [VBA]
    Dim Info() As Variant
    Dim ijk As Integer
    Dim x As integer

    For x = 1 to 1000
    If (some event) Then
    ReDim Preserve Info(1 To ijk, 1 To 2)
    Info(ijk, 1) = Id
    Info(ijk, 2) = TempW(x, 5)
    ijk = ijk + 1
    Else End if
    Next x



    [/VBA]

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    mferrisi,

    "ijk" is never being given an initial value, so at first run you are dimming info as "(1 to 0, 1 to 2)"

    That being said, you'll have to switch the "1 to 2" and "1 to ijk", as you can only increase the highest dimension of the array:[vba]Dim Info() As Variant
    Dim ijk As Integer
    Dim x As Integer

    ijk = 1
    For x = 1 To 1000
    If (some event) Then
    ReDim Preserve Info(1 To 2, 1 To ijk)
    Info(1, ijk) = ID
    Info(2, ijk) = TempW(x, 5)
    ijk = ijk + 1
    End If
    Next x[/vba] Lastly, dim 'x' and 'ijk' as Long, as you're using VBA for this and all Integers get converted to Long Integers at runtime anyways in VBA for office 2000+
    Matt

Posting Permissions

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