W
William Benson
Hi, recently learned the joy of pasting an entire 2D array into an excel
range in one step instead of looping through array elements and pasting into
individual cells. I use a collection to populate the array and the downside
is everything comes out as strings, regardless of the data type of the items
originally read into the collection. Also case sensitivity is ignored (makes
me very unhappy). Finally, am not aware of a "quick" method in VBA to sort
an array of several hundred thousand elements ... would sorting a collection
be quicker? Below is the code, if helpful in answering, thanks.
Bill
Sub ProcessCollection(mCol As Collection, strSht As String)
Dim i As Long, j As Long
Dim PasteRange As Range
Dim PasteColumnCount As Long
Dim PasteRowCount As Long
Dim PasteArray
Dim Itm
Dim Sht As Worksheet
If mCol.Count Mod 65536 = 0 Then
PasteColumnCount = mCol.Count \ 65536
Else
PasteColumnCount = mCol.Count \ 65536 + 1
End If
'Set up Row Dimension from Test1
If PasteColumnCount > 1 Then
PasteRowCount = 65536
Else
PasteRowCount = mCol.Count
End If
'Define an array which is large enough to hold all the elements
'of the collection which resulted from Test1
ReDim PasteArray(1 To PasteRowCount, 1 To PasteColumnCount)
i = 1: j = 0
For Each Itm In mCol
If j + 1 > 65536 Then
j = 1
i = i + 1
Else
j = j + 1
End If
PasteArray(j, i) = mCol(CStr(Itm))
Next Itm
Sheets.Add
ActiveSheet.Name = strSht
Set PasteRange = Worksheets(strSht).Range(Worksheets(strSht).Range("A1"),
Worksheets(strSht).Range("A1").Offset(PasteRowCount - 1, PasteColumnCount -
1))
PasteRange.Value = PasteArray
End Sub
range in one step instead of looping through array elements and pasting into
individual cells. I use a collection to populate the array and the downside
is everything comes out as strings, regardless of the data type of the items
originally read into the collection. Also case sensitivity is ignored (makes
me very unhappy). Finally, am not aware of a "quick" method in VBA to sort
an array of several hundred thousand elements ... would sorting a collection
be quicker? Below is the code, if helpful in answering, thanks.
Bill
Sub ProcessCollection(mCol As Collection, strSht As String)
Dim i As Long, j As Long
Dim PasteRange As Range
Dim PasteColumnCount As Long
Dim PasteRowCount As Long
Dim PasteArray
Dim Itm
Dim Sht As Worksheet
If mCol.Count Mod 65536 = 0 Then
PasteColumnCount = mCol.Count \ 65536
Else
PasteColumnCount = mCol.Count \ 65536 + 1
End If
'Set up Row Dimension from Test1
If PasteColumnCount > 1 Then
PasteRowCount = 65536
Else
PasteRowCount = mCol.Count
End If
'Define an array which is large enough to hold all the elements
'of the collection which resulted from Test1
ReDim PasteArray(1 To PasteRowCount, 1 To PasteColumnCount)
i = 1: j = 0
For Each Itm In mCol
If j + 1 > 65536 Then
j = 1
i = i + 1
Else
j = j + 1
End If
PasteArray(j, i) = mCol(CStr(Itm))
Next Itm
Sheets.Add
ActiveSheet.Name = strSht
Set PasteRange = Worksheets(strSht).Range(Worksheets(strSht).Range("A1"),
Worksheets(strSht).Range("A1").Offset(PasteRowCount - 1, PasteColumnCount -
1))
PasteRange.Value = PasteArray
End Sub