PDA

View Full Version : Solved: Variable dimension of a matrix



pepe90
07-25-2011, 06:02 AM
Hello, does somebody know how can I change the number of rows of a matrix? Here is what I thought that could work:
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



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

Kenneth Hobs
07-25-2011, 06:05 AM
Switch your thinking to use:
ReDim Preserve MATRIZ(1 To 4, 1 To maqs_encontradas)

pepe90
07-25-2011, 06:16 AM
Thanks a lot Kenneth, it worked. Could you explain me please why it had to be that way?

Kenneth Hobs
07-25-2011, 06:35 AM
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 (http://www.vbaexpress.com/forum/HV10383569.htm) used to preserve the data in an existing array (http://www.vbaexpress.com/forum/HV10383569.htm) when you change the size of the last dimension.