D
Dan Thompson
I have recently found a function on the interent that will remove duplicate
values within any array. It works just fine for a single dimensional array
but I would like to edit and change the code to work for Multi-Dimensional
array to support a minimum of 2 dimentional arrays. I only need it to find
any duplicate values within the first dimension of the array and than remove
any values corisponding to the same element number that the duplicate value
was found in the 1st dimention of the array and remove them from the 2nd and
3rd dimention of the array as well.
For example:
MyArray(1,2)
----------------------
| 0 | 0 | Hello |
| 1 | 0 | XX |
| 0 | 1 | Bye |
| 1 | 1 | YY |
| 0 | 2 | Hello |
| 1 | 2 | ZZ |
----------------------
So how it should work on the above sample array is that it would find look
for duplicate value in 1st dimention and find the value in 0,2 is a duplicate
of 0,0 and it will remove the 0,2 value and than since it found a duplicate
value in 0,2 and removed it, It would also than remove any value in same
corrisponding element row from the other dimentions i the case of the sample
above it would also not only remove 0,2 but it would remove 1,2 as well even
though 1,2 is not a duplicate
value.
Here is my currently working function based on just single dimetional arrays
Public Function RemoveDuplicates(ByRef SourceArray As Variant)
Dim Values As Collection
Dim Value As Variant
Dim Index1 As Long
Dim Index2 As Long
Set Values = New Collection
Index2 = LBound(SourceArray)
On Error Resume Next
For Index1 = LBound(SourceArray) To UBound(SourceArray)
Value = Empty
Value = Values(VarType(SourceArray(Index1)) & "|" & SourceArray(Index1))
If IsEmpty(Value) And Not Len(SourceArray(Index1)) = 0 Then
Values.Add SourceArray(Index1), VarType(SourceArray(Index1)) & "|"
& SourceArray(Index1)
SourceArray(Index2) = SourceArray(Index1)
Index2 = Index2 + 1
End If
Next Index1
On Error GoTo 0
If Index2 = 1 Then
SourceArray = Empty
Else
ReDim Preserve SourceArray(LBound(SourceArray) To Index2 - 1)
End If
End Function
I hope someone can help me find a solution.
Thanks,
Dan Thompson
values within any array. It works just fine for a single dimensional array
but I would like to edit and change the code to work for Multi-Dimensional
array to support a minimum of 2 dimentional arrays. I only need it to find
any duplicate values within the first dimension of the array and than remove
any values corisponding to the same element number that the duplicate value
was found in the 1st dimention of the array and remove them from the 2nd and
3rd dimention of the array as well.
For example:
MyArray(1,2)
----------------------
| 0 | 0 | Hello |
| 1 | 0 | XX |
| 0 | 1 | Bye |
| 1 | 1 | YY |
| 0 | 2 | Hello |
| 1 | 2 | ZZ |
----------------------
So how it should work on the above sample array is that it would find look
for duplicate value in 1st dimention and find the value in 0,2 is a duplicate
of 0,0 and it will remove the 0,2 value and than since it found a duplicate
value in 0,2 and removed it, It would also than remove any value in same
corrisponding element row from the other dimentions i the case of the sample
above it would also not only remove 0,2 but it would remove 1,2 as well even
though 1,2 is not a duplicate
value.
Here is my currently working function based on just single dimetional arrays
Public Function RemoveDuplicates(ByRef SourceArray As Variant)
Dim Values As Collection
Dim Value As Variant
Dim Index1 As Long
Dim Index2 As Long
Set Values = New Collection
Index2 = LBound(SourceArray)
On Error Resume Next
For Index1 = LBound(SourceArray) To UBound(SourceArray)
Value = Empty
Value = Values(VarType(SourceArray(Index1)) & "|" & SourceArray(Index1))
If IsEmpty(Value) And Not Len(SourceArray(Index1)) = 0 Then
Values.Add SourceArray(Index1), VarType(SourceArray(Index1)) & "|"
& SourceArray(Index1)
SourceArray(Index2) = SourceArray(Index1)
Index2 = Index2 + 1
End If
Next Index1
On Error GoTo 0
If Index2 = 1 Then
SourceArray = Empty
Else
ReDim Preserve SourceArray(LBound(SourceArray) To Index2 - 1)
End If
End Function
I hope someone can help me find a solution.
Thanks,
Dan Thompson