For some reason I’m getting a ‘Runtime 13’ error when I execute the following code.
Dim N_1 As Variant
Worksheets("Trucks").Activate
Range("G9").Activate
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then Exit Do
Loop
ActiveCell.Offset(-1, 0).Select
N_1 = Range(ActiveCell, "G9")
With CreateObject("scripting.dictionary")
.comparemode = vbTextCompare
For Each v1 In N_1
If Not IsEmpty(v1) Then
If Not .exists(v1) Then .Add v1, Nothing
End If
Next
z1 = .keys
End With
Range(...)returns an object type soSetmust be used:Set N_1 = Range(ActiveCell, "G9")While
Range(...)does return an object type, assigning it to aVariantwithout usingSetproduces a 2D variant array of the cell values in the range. This works OK with theFor Each v1 in N_1part and with theIsEmpty(v1)part.Therefore, I’m not 100% sure why you got error 13 in the first place (assuming that
v1andz1are also declared asVariant). Indeed if you useSetyou’ll also have to alter your loop which populates the dictionary to usev1.Valueinstead of justv1If you are subsequently using
N_1and expecting it to be aRangeobject then this would explain things but the code as presented above runs OK for me if everything is declared as aVariant: