Skip to content

Instantly share code, notes, and snippets.

@maximpertsov
Last active January 21, 2016 20:10
Show Gist options
  • Save maximpertsov/6995b6b3a23e50424172 to your computer and use it in GitHub Desktop.
Save maximpertsov/6995b6b3a23e50424172 to your computer and use it in GitHub Desktop.
Convert a VBA collection to an array
Function CollectionToArray(C As Collection, Optional StartIdx As Long, Optional Size As Long) As Variant
' Convert a collection into an array
Dim A() As Variant
Dim Ci As Variant ' Collection element
Dim i As Long
' Make sure Size is not less than collection size
If Size < C.Count Then Size = C.Count
' Set starting index (zero by default)
i = StartIdx
' Transfer collection to array
ReDim A(i To i + Size - 1)
For Each Ci In C
A(i) = Ci
i = i + 1
Next
CollectionToArray = A
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment