Consulting

Results 1 to 4 of 4

Thread: Solved: Variable dimension of a matrix

  1. #1
    VBAX Regular
    Joined
    Jul 2011
    Posts
    33
    Location

    Solved: Variable dimension of a matrix

    Hello, does somebody know how can I change the number of rows of a matrix? Here is what I thought that could work:
    [vba]With Sheets("DATOS")
    j = 2
    Do While .Cells(j, 2) <> ""
    If .Cells(j, 2) = maquinas(i) Then
    maqs_encontradas = maqs_encontradas + 1
    ReDim Preserve MATRIZ(1 To maqs_encontradas, 1 To 4)
    For k = 1 To 4
    MATRIZ(maqs_encontradas, k) = .Cells(j, k + 1)
    Next
    End If
    j = j + 1
    Loop
    End With

    [/vba]

    once maqs_encontradas is 2, it throws me a subindex error on the ReDim line. I've done the same thing changing the size of a 1-dimensional array and worked just fine. Thanks in advance

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Switch your thinking to use:
    [vba]ReDim Preserve MATRIZ(1 To 4, 1 To maqs_encontradas) [/vba]

  3. #3
    VBAX Regular
    Joined
    Jul 2011
    Posts
    33
    Location
    Thanks a lot Kenneth, it worked. Could you explain me please why it had to be that way?

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Redim can only work on the end dimension of multiple dimensions. If you press F1 in the VBE with the cursor in or near Redim:
    Part Description Preserve Optional. Keyword used to preserve the data in an existing array when you change the size of the last dimension.

Posting Permissions

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