Home > How-To Library > Arrays, Collections, Lists

How to delete an item in an array

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
Sub DeleteArrayItem(arr As Variant, index As Long) Dim I As Long For I = index To UBound(arr) - 1 arr(I) = arr(I + 1) Next ' VB will convert this to 0 or to an empty string. arr(UBound(arr)) = Empty End Sub 'Or (Using API) Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, _ source As Any, ByVal numBytes As Long) Sub DeleteArrayItemLong(arr() As Long, index As Long) If index < UBound(arr) Then CopyMemory arr(index), arr(index + 1), (UBound(arr) - index) * LenB(arr(index)) End If arr(index) = Empty End Sub

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.