Public Function SliceLng02( _
SourceArray() As Long, _
TargetArray() As Long, _
Optional ByVal Start As Long = 0, _
Optional ByVal Count As Long = -1, _
Optional ByVal Exclude As Boolean = False _
) As Boolean
' by Ratmoler HAMSTAK, hamstak@hotmail.com, 20011124
Dim lBoundSourceArray As Long
Dim uBoundSourceArray As Long
Dim lBoundTargetArray As Long
Dim uBoundTargetArray As Long
Dim Offset As Long
Dim lenTargetArray As Long
Dim x As Long
On Error GoTo exit_SliceLng02
lBoundSourceArray = LBound(SourceArray)
uBoundSourceArray = UBound(SourceArray)
lBoundTargetArray = lBoundSourceArray
If Start < lBoundSourceArray Then
Start = lBoundSourceArray
ElseIf Start > uBoundSourceArray Then
If Exclude Then
Exclude = False
Start = lBoundSourceArray
Count = uBoundSourceArray - lBoundSourceArray + 1
Else
Erase TargetArray
Exit Function
End If
End If
If Count < 0 Then
Count = uBoundSourceArray - Start + 1
ElseIf Count = 0 Then
If Exclude Then
Exclude = False
Start = lBoundSourceArray
Count = uBoundSourceArray - Start + 1
Else
Erase TargetArray
Exit Function
End If
End If
If Start + Count > uBoundSourceArray Then
If Start = lBoundSourceArray Then
If Exclude Then
Erase TargetArray
Exit Function
Else
Count = uBoundSourceArray - lBoundSourceArray + 1
End If
Else
Count = uBoundSourceArray - Start + 1
End If
End If
If Exclude Then
uBoundTargetArray = uBoundSourceArray - Count
ReDim TargetArray(lBoundTargetArray To uBoundTargetArray)
If Start > lBoundSourceArray Then
CopyMemory _
ByVal VarPtr(TargetArray(lBoundTargetArray)), _
ByVal VarPtr(SourceArray(lBoundSourceArray)), _
VarPtr(SourceArray(Start)) - VarPtr(SourceArray(lBoundSourceArray))
End If
If (Start + Count - 1) < uBoundSourceArray Then
CopyMemory _
ByVal VarPtr(TargetArray(Start)), _
ByVal VarPtr(SourceArray(Start + Count)), _
VarPtr(TargetArray(uBoundTargetArray)) - VarPtr(TargetArray(Start)) + 4
End If
Else ' Specified segment (start,Count) to be returned
Offset = Start - lBoundSourceArray
uBoundTargetArray = lBoundSourceArray + Count - 1
If uBoundTargetArray > uBoundSourceArray Or uBoundTargetArray < lBoundTargetArray Then
uBoundTargetArray = uBoundSourceArray - Offset
End If
ReDim TargetArray(lBoundTargetArray To uBoundTargetArray)
CopyMemory _
ByVal VarPtr(TargetArray(lBoundTargetArray)), _
ByVal VarPtr(SourceArray(lBoundTargetArray + Offset)), _
VarPtr(TargetArray(uBoundTargetArray)) - VarPtr(TargetArray(lBoundTargetArray)) + 4
End If
SliceLng02 = True
exit_SliceLng02:
End Function
|