Sub Test_RemoveDupesArray2D()
Sheet1.Range("A12:AH100").ClearContents
Dim Arr: Arr = Sheet1.Range("A1:Q2").Value
Call RemoveDupesArray2D(Arr, NumRC:=2, isRow:=False, _
isTrans:=False, CellsResult:=Sheet1.Range("A4"))
Call RemoveDupesArray2D(Arr, NumRC:=2, isRow:=False, _
isTrans:=True, CellsResult:=Sheet1.Range("S4"))
Call RemoveDupesArray2D(Arr, NumRC:=1, isRow:=True, _
isTrans:=False, CellsResult:=Sheet1.Range("A22"))
Call RemoveDupesArray2D(Arr, NumRC:=1, isRow:=True, _
isTrans:=True, CellsResult:=Sheet1.Range("S22"))
End Sub
Function RemoveDupesArray2D(DataArr As Variant, Optional isRow As Boolean = True, _
Optional NumRC = 1, Optional isTrans As Boolean = False, _
Optional CellsResult As Range) As Variant
Dim rArr, iStep&, i&, j&, LA1&, UA1&, LA2&, UA2&, iCol&, iRow&, rRow&, rCol&
LA1 = LBound(DataArr): UA1 = UBound(DataArr)
LA2 = LBound(DataArr, 2): UA2 = UBound(DataArr, 2)
ReDim rArr(IIf(isTrans, LA2, LA1) To IIf(isTrans, UA2, UA1), _
IIf(isTrans, LA1, LA2) To IIf(isTrans, UA1, UA2))
LA1 = IIf(isRow, LA1, LA2): UA1 = IIf(isRow, UA1, UA2)
LA2 = IIf(isRow, LA2, LBound(DataArr)): UA2 = IIf(isRow, UA2, UBound(DataArr))
With CreateObject("Scripting.Dictionary")
For i = LA1 To UA1
iRow = IIf(isRow, i, NumRC): iCol = IIf(isRow, NumRC, i)
If Not .Exists(DataArr(iRow, iCol)) Then
iStep = iStep + 1
For j = LA2 To UA2
rRow = IIf(isRow, iStep, j): rCol = IIf(isRow, j, iStep)
rArr(IIf(isTrans, rCol, rRow), IIf(isTrans, rRow, rCol)) = DataArr(IIf(isRow, i, j), IIf(isRow, j, i))
Next j
.Item(DataArr(iRow, iCol)) = 1
End If
Next
RemoveDupesArray2D = rArr
End With
If Not CellsResult Is Nothing Then
CellsResult.Parent.Cells(CellsResult.Row, CellsResult.Column) _
.Resize(IIf(isTrans, rCol, rRow), IIf(isTrans, rRow, rCol)).Value = rArr
End If
End Function