Home » Arrays, Collections, Lists » How to insert an item in an array
How to insert an item in an array
Posted on October 23, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 'Arrays, Collections, Lists - How to insert an item in an array Sub InsertArrayItem(arr As Variant, index As Long, newValue As Variant) Dim I As Long For I = UBound(arr) - 1 To index Step -1 arr(I + 1) = arr(I) Next arr(index) = newValue End Sub 'Or (Using API) Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, _ source As Any, ByVal numBytes As Long) Sub InsertArrayItemLong(arr() As Long, index As Long, newValue As Long) ' We let VB evaluate the size of each item using LenB(). If index < UBound(arr) Then CopyMemory arr(index + 1), arr(index), (UBound(arr) - index) * LenB(arr(index)) End If arr(index) = newValue End Sub |
Enjoy this article?
Filed under: Arrays, Collections, Lists
Leave a comment