My macro goes through a range, looping by columns, finds where the numeric data starts in each column and stores the ranges in a jagged array (the "matrix" variant in the code).
After that, I would like to return the entire matrix to a range in another worksheet. If I try to assign "matrix(1)" to the range where I want it to be put, it works fine, but if I try to assign the entire "matrix" to a range, I get blank cells.
How could I return all of the values in "matrix" to a range at once, without using loops?
This is the source data, through which the code loops:
I would like that all of the rows of "matrix" would be returned as this:
Here is my code:
Sub MyMatrix()
Dim wb1 As Workbook
Set wb1 = ActiveWorkbook
Dim wsNSA As Worksheet
Set wsNSA = wb1.Worksheets("NSA")
Dim wsSA As Worksheet
Set wsSA = wb1.Worksheets("SA")
Dim col As Range
Dim matrix() As Variant
'LR is the Last row and LC is the last column with data
LR = wsNSA.Cells(1, 1).End(xlDown).Row
LC = wsNSA.Cells(LR, 1).End(xlToRight).Column
'Loops through columns and finds the row where numeric data begins
For Each col In wsNSA.Range(wsNSA.Cells(1, 2), wsNSA.Cells(LR, LC)).Columns
wsNSA.Activate
nsa = wsNSA.Range(wsNSA.Cells(1, col.Column), wsNSA.Cells(LR, col.Column))
num_linha = Application.Match(True, Application.Index(Application.IsNumber(nsa), 0), 0)
nsa = wsNSA.Range(wsNSA.Cells(num_linha, col.Column), wsNSA.Cells(LR, col.Column))
'The range starts in the column B in the worksheet, so the matrix ubound is 'col.column -1
ReDim Preserve matrix(1 To col.Column - 1)
matrix(col.Column - 1) = nsa
Next
wsSA.Range(wsSA.Cells(3, 2), wsSA.Cells(LR, LC)) = matrix
End Sub
If you are you willing to forget the requirement that the output should not be written inside a loop, the following code would probably do what you are trying to do: