First off I would say this only viable given you're using XL2007 - in earlier versions you were limited to 256 columns and given your real 1044x1044 matrix could theoretically need to be transposed into one row you would not be able to do this.
I would say first off that whether you opt for VBA or formulae you're best bet will be to first store your matrix in a vector ... eg using example of A1:F6 you create a vector of A1:A36 on another sheet, it will I think be much easier to work with the vector to create the final matrix than with the original matrix ... ie you could achieve this with formulae...
If you were to create vector impression of initial matrix on Sheet2 where initial matrix Sheet1!A1:F6 like so:
Sheet2!A1:
=INDEX(Sheet1!$A$1:$F$36,1+INT((ROWS(A$1:A1)-1)/6),1+MOD(ROWS(A$1:A1)-1,6))
copied down to A36
And we were then to recreate matrix per row requirement of user, say on Sheet3 then with row requirement entered into A1 (and validated such that it is indeed a multiple of the rows in the vector) then
A1: 3 (entered by user)
B1: =COUNT(Sheet2!A:A)/A1
C1: =MOD(B1,1)
(where C1 is the check that A1 is multiple - ie even spread possible)
we can in turn create the requisite matrix using:
A2: =IF(OR($C$1,ROWS(A$2:A2)>$A$1,COLUMNS($A2:A2)>$B$1),"",INDEX(Sheet2!$A:$A,COLUMNS($A2:A2)+($B$1*(ROWS(A$2:A2)-1))))
applied across matrix - where matrix is square of original
(using example 36 x 36 given could be 1 row or 36 row requirement from user)
I confess I've not tested this out on a large matrix and I suspect performance would be poor and would warrant VBA but I just wanted to illustrate that by using a vector things become a little simpler.
In VBA terms it would I think be simply a case of iterating the vector, selecting the range of values to be copied from the vector and pasting into next blank row on results tab, something like:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngData As Range, lngRowI As Long, dblDiv As Double
On Error GoTo ExitPoint
If Target.Address <> "$A$1" Or Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
With Sheets("Sheet2"): Set rngData = .Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp)): End With
dblDiv = rngData.Rows.Count / Target.Value
If Int(dblDiv) <> dblDiv Then
MsgBox "Invalid Row Requirement - Not Flat", vbCritical, "Error"
GoTo ExitPoint
Else
ActiveSheet.UsedRange.Offset(1).Clear
For lngRowI = 1 To Target.Value Step 1
Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, dblDiv).Value = Application.Transpose(rngData.Cells(1 + (dblDiv * (lngRowI - 1)), 1).Resize(dblDiv))
Next lngRowI
End If
ExitPoint:
Set rngData = Nothing
Application.EnableEvents = True
End Sub
the above utilising the Change event on cell A1 such that when A1 is altered the matrix repopulates.
I've attached a file which illustrates all of the above - given this is only viable in XL2007 it is .xlsm format
Bookmarks