Cut Elements Of an Array

Search

 by Remas Wojciechowski

This function cuts a number [intCount] of elements out of an array [aToCut] and returns a new array populated with these elements. The original array variable is overwritten by a new array that holds all remaining elements. The direction is determined by the [constDirection] parameter (see the comment lines).

'Const ARRAY_CUT_FROMTOP = 0
'Const ARRAY_CUT_FROMBOTTOM = 1
Function ARRAY_CutElements(ByRef aToCut, ByVal intCount, ByVal constDirection)
   Dim aRemainder()
   Dim aCut()
   Dim intLB, intUB
   Dim i
   intLB = LBound(aToCut)
   intUB = UBound(aToCut)
   If intCount >= intUB + 1 Then
      ARRAY_CutElements = aToCut
      aToCut = Null
   Else
      ReDim aRemainder(intUB - intCount)
      ReDim aCut(intCount - 1)
      Select Case constDirection
         Case ARRAY_CUT_FROMTOP
            For i = intLB To intUB
               If i <= intCount - 1 Then
                  aCut(i) = aToCut(i)
               Else
                  aRemainder(i - intCount) = aToCut(i)
               End If
            Next
         Case ARRAY_CUT_FROMBOTTOM
            For i = intLB To intUB
               If i < intUB - intCount Then
                  aRemainder(i) = aToCut(i)
               Else
                  aCut(i - intUB + intCount) = aToCut(i)
               End If
            Next
      End Select
      aToCut = aRemainder
      ARRAY_CutElements = aCut
   End If
End Function