First, a little background.
I am trying to combine multiple 2D arrays. I usually scrolled through each element of a new array and added them to existing arrays or placed the values โโof arrays on a separate sheet and created a new array from it, but I work with big data. I recently found the CopyMemory function and am excited about it, first tested it on simple pieces of data.

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Sub Test()
Dim varr0(), varr1(), Border As Long
varr0 = Application.Transpose(Range("a1").CurrentRegion.Value)
Border = UBound(varr0, 2)
varr1 = Application.Transpose(Range("a21").CurrentRegion.Value)
ReDim Preserve varr0(1 To UBound(varr0, 1), 1 To UBound(varr0, 2) + UBound(varr1, 2))
CopyMemory varr0(1, Border + 1), varr1(1, 1), UBound(varr1, 1) * UBound(varr1, 2) * 16
Range(Cells(1, 10), Cells(1, 10).Offset(UBound(varr0, 2) - 1, UBound(varr0, 1) - 1)).Value = Application.Transpose(varr0)
End Sub
It is clear that this was a success (or so I thought), and I decided to work with pieces of my actual data, from there it went down.
Sub Test_2()
Dim varr0(), varr1(), Border As Long, ws As Worksheet
varr0 = Application.Transpose(ThisWorkbook.Worksheets("Sheet1").Range("a1").CurrentRegion.Value)
Border = UBound(varr0, 2)
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
varr1 = Application.Transpose(ws.Range("a1").CurrentRegion.Value)
ReDim Preserve varr0(1 To UBound(varr0), 1 To UBound(varr0) + UBound(varr1))
CopyMemory varr0(1, Border + 1), varr1(1, 1), UBound(varr1, 1) * UBound(varr1, 2) * 16
Border = UBound(varr0, 2)
End If
Next
ThisWorkbook.Worksheets("ws1").Range(Cells(1, 11), Cells(1, 11).Offset(UBound(varr0, 2) - 1, UBound(varr0, 1) - 1)).Value = Application.Transpose(varr0)
End Sub
, , Excel ( , , - ( cap)).
, , , .
Per Variant
16 .
():
:
, , .
Sub Test_6()
Dim varr0(), varr1(), Border As Long, ws As Worksheet, MemUsage As Long
varr0 = Application.Transpose(ThisWorkbook.Worksheets("Sheet1").Range("a1").CurrentRegion.Value)
Border = UBound(varr0, 2)
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
varr1 = Application.Transpose(ws.Range("a1").CurrentRegion.Value)
ReDim Preserve varr0(1 To UBound(varr0, 1), 1 To UBound(varr0, 2) + UBound(varr1, 2))
MemUsage = VarPtr(varr1(UBound(varr1, 1), UBound(varr1, 2))) - VarPtr(varr1(1, 1))
CopyMemory varr0(1, Border + 1), varr1(1, 1), MemUsage + 16 + Len(varr1(UBound(varr1, 1), UBound(varr1, 2)))
Border = UBound(varr0, 2)
End If
Next
ThisWorkbook.Worksheets("Sheet1").Range(Cells(1, 11), Cells(1, 11).Offset(UBound(varr0, 2) - 1, UBound(varr0, 1) - 1)).Value = Application.Transpose(varr0)
End Sub
, , CopyMemory
Excel .