Hello…
I think while this is still fresh in my mind I will check the case of an array with more than one column, as I am sure the question will come up later.. and ..
a stitch in time saves nine… :)
( and as p45 reminded me of that address way to get the column Letter, I don’t need to go crazy trying to do multiple transposing, so that saves me a lot of headaches and makes things a lot easier.…)
I’ll just give the pretty results in the post.
The full workings are in the second worksheet of the attached file, both in the worksheet spreadsheet and in the worksheet code module.
ItsAllSoBeautiful.xls
_.__________________________________
So, if you want to Split a 2dArray
arrIn() Into Multiple Equal 2d Arrays,
but have all those output arrays put into a single array,
arrOut() , then this should do it
Code: Select all
Sub Split2dArrayIntoMultipleEqual2dArrays()
' test range
Dim arrIn() As Variant: Let arrIn() = Me.Range("B2:D6").Value2
' _______________________________________
Dim Ub As Long, nRows As Long, ClmCnt As Long
Let Ub = 5 ' the row number of the input range
Let nRows = 2 ' the row number you want to slice the range into
Let ClmCnt = UBound(arrIn(), 2) ' 3 ' The column number in the input range
Dim arrOut() As Variant
Let arrOut() = Application.Index(arrIn(), Evaluate("=((INT((COLUMN(A:" & Split(Cells(1, (((Int((Ub - 1) / nRows)) + 1)) * ClmCnt).Address, "$")(1) & ")-1)/" & ClmCnt & "))*" & nRows & ")+ROW(1:" & nRows & ")"), Evaluate("=If({1},MOD(COLUMN(A:" & Split(Cells(1, (((Int((Ub - 1) / nRows)) + 1)) * ClmCnt).Address, "$")(1) & ")-1," & ClmCnt & ")+1)"))
' demo output, top left is P2
With Range("P2").Resize(UBound(arrOut(), 1), UBound(arrOut(), 2))
Let .Value = arrOut()
Let .Value = Evaluate("=IF(ISERROR(" & .Address & "),""""," & .Address & ")")
End With
End Sub
Here next is a slight variation of the above for doing something similar. In this case you have a spreadsheet range, and you want to do a similar splitting.
Code: Select all
Sub EvalSpreadsheetIndex()
Dim Ub As Long, nRows As Long, ClmCnt As Long
Let Ub = 5 ' the row number of the input range
Let nRows = 2 ' the row number you want to slice the range into
Let ClmCnt = 3 ' The column number in the input range
With Me.Range("P2").Resize(nRows, ((Int((Ub - 1) / nRows)) + 1) * ClmCnt)
Let .Value = Evaluate("=If({1},INDEX(B2:D6,N(If(1,((INT((COLUMN(A:" & Split(Cells(1, (((Int((Ub - 1) / nRows)) + 1)) * ClmCnt).Address, "$")(1) & ")-1)/" & ClmCnt & "))*" & nRows & ")+ROW(1:" & nRows & "))),N(If(1,MOD(COLUMN(A:" & Split(Cells(1, (((Int((Ub - 1) / nRows)) + 1)) * ClmCnt).Address, "$")(1) & ")-1," & ClmCnt & ")+1))))")
Let .Value = Evaluate("=IF(ISERROR(" & .Address & "),""""," & .Address & ")")
End With
End Sub
Both use a typical test range to demo what’s going on.
This is your input:
Range B2-D6.JPG
This is the output
Output Top Left P2.JPG
I have not tested thoroughly, yet. I just wanted to put the stuff somewhere useful to get it from later, and maybe someone can use it in the meantime - The thread title fits nicely so me or someone should be able to find it later
I think these sort of things are doing something like a spreadsheet array formula, and maybe could be an alternative for a “big” range.
For a small range you might as well just do a formula, and for a much larger range, other VBA ways are probably better.
Maybe these sort of things are a nice alternative for a middle size requirement, maybe
Alan
You do not have the required permissions to view the files attached to this post.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also