CODE WORKS CORRECTLY. MODIFIED BASED ON HELP FROM RESPONSES.
I have the following code to remove duplicates from a array, MyArray. The code gets a debugging error at: d(MyArray(i)) = 1. The error is subscript out of range. Not sure what is causing this and what is wrong with my code.
Sub DataStats1()
Dim Range1 As Range
Dim MyArray As Variant
Set Range1 = Application.InputBox("Select Range1:", Title:="Set Data Range", Type:=8)
Range1.Select
MyArray = Application.Transpose(Application.Transpose(Range1.Value))
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
For Each el In MyArray
d(el) = 1
Next
Dim v As Variant
v = d.Keys()
For i = 1 To UBound(v)
MsgBox v(i)
Next i
End Sub
MyArrayis always 2D array, even if you select single row/column range. E.g. for single column range you should used(MyArray(i,1)) = 1insteadd(MyArray(i)) = 1. But I suggets you to changeFor i = LBound(MyArray) To UBound(MyArray)to sayFor Each el in MyArrayand thend(el) = 1v. Perhaps you meanv = d.Keys().