So this would be a code example using the simplest of my Functions
http://www.excelforum.com/tips-and-t...ml#post4378086
But note, I apply my Functions to a version of Norie’s code. I do this because your code does not work anyway
( Or rather i do not think it does what you want. Nories code does do, I think, what you want.
Code:
Sub ArrayTestingNorieAlan() ' http://www.excelforum.com/excel-programming-vba-macros/1159484-redim-array-inside-nested-loops.html
Dim cell As Range
Dim MyArray() As Variant
Dim NumCols As Long
Dim NumRows As Long
Dim i As Long
Dim j As Long
Dim Cnt As Long
NumCols = 3
NumRows = Cells(Rows.Count, 1).End(xlUp).Row
ReDim MyArray(1 To NumRows, 1 To NumCols) 'Need to give some original size, or my Fúnction will error as it needs a size in order to resize.
For Each cell In Worksheets("Sheet1").Range("A1:A" & NumRows).Cells
If cell.Value = "A1" Then
Cnt = Cnt + 1
'ReDim Preserve MyArray(1 To NumCols, 1 To cnt)
Let MyArray() = SHimpfGlifiedReDimRow_2(MyArray(), Cnt)
For j = 1 To NumCols
'MyArray(j, Cnt) = Cells(cell.Row, j).Value
Let MyArray(Cnt, j) = Cells(cell.Row, j).Value
Next j
End If
Next cell
'MyArray = Application.Transpose(MyArray)
'print results
For i = 1 To Cnt
For j = 1 To NumCols
Debug.Print MyArray(i, j)
Next j
Next i
End Sub
'
' http://www.excelforum.com/tips-and-tutorials/1137811-functions-to-re-dim-preserve-first-dimension-in-2-d-array-and-transpose-byvalue.html#post4378086
Public Function SHimpfGlifiedReDimRow_2(ByVal arrIn As Variant, ByVal FirstIndicieIncreaseTo As Long)
Let arrIn = SHimpfGlifiedFT(arrIn) 'Note you cannot simplify further and use SHimpfGlifiedReDimRow as LHS here and furhter in code as Re Dim errors
ReDim Preserve arrIn(1 To UBound(arrIn, 1), 1 To FirstIndicieIncreaseTo)
Let SHimpfGlifiedReDimRow_2 = SHimpfGlifiedFT(arrIn)
End Function
Public Function SHimpfGlifiedFT(ByVal inArr As Variant)
Dim outArr() As Variant: ReDim outArr(1 To UBound(inArr, 2), 1 To UBound(inArr, 1))
Dim j As Long, i As Long
For j = 1 To UBound(inArr, 1)
For i = 1 To UBound(inArr, 2)
outArr(i, j) = inArr(j, i)
Next i
Next j
Let SHimpfGlifiedFT = outArr()
End Function
Alan
Bookmarks