A bit of background first.
I'm trying to merge multiple 2D arrays. Usualy I would loop through each element of new array and add them to existing array or place arrays' values on separate sheet and create new array from it but I'm working with large data.
Not long ago I found CopyMemory function and got realy excited by it, I've tested it on simple chunks of data first.

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
Clearly it was a success (or so I thought) and I decided to work with pieces of my actual data, from there it went downhill.
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
Whenever I execute it, Excel crashes (it doesn't identify the error but warns that something went wrong (thanks cap)).
The only thing I can think of is that new data had strings in it.
Per Source Variant requires only 16 bytes.
My question(s) is:
- How do I make it work as intended?
- What flaw of my logic causes Excel to crash?
- Is it possible to merge two arrays without
- Loops (or at least without iterating through whole array)
- Use of sheets
Update:
It seems I was calculating memory to copy incorrectly so I slightly modified my macro.
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
In watch window I can clearly see that merge was successful, but shortly after CopyMemory line Excel crashes again.