4

I currently would use something like this either with Range, Cells or the like many different ways same basic principle.

Range("A1", Range("A1").End(xlDown)).AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=Range("IV1"), Unique:=True

Dim myArr as Variant 
myArr = Range("IV1", Range("IV1").End(xlDown))
Columns("IV").Delete

Is there a way to directly load those unique values into any type of object in VBA without the need to copy to another location?

GSerg
  • 76,472
  • 17
  • 159
  • 346
user2140261
  • 7,855
  • 7
  • 32
  • 45

1 Answers1

6

You can use a Collection Object to create unique entries. For example

Sub Sample()
    Dim Col As New Collection
    Dim itm
    Dim i As Long
    Dim CellVal As Variant

    '~~> Lets say looping through Row 1 to 22 For 
    '~~> Range("A1:A22") as mentioned in your recent comment
    For i = 1 To 22
        CellVal = Sheets("Sheet1").Range("A" & i).Value
        On Error Resume Next
        Col.Add CellVal, Chr(34) & CellVal & Chr(34)
        On Error GoTo 0
    Next i

    For Each itm In Col
        Debug.Print itm
    Next
End Sub

ScreenShot:

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • So, a collection will throw an error if the same value is added twice? – user2140261 Apr 03 '13 at 22:24
  • No it will not since we are using OERN – Siddharth Rout Apr 03 '13 at 22:24
  • OERN = `On Error Resume Next` – Siddharth Rout Apr 03 '13 at 22:25
  • 2
    But the collection is still throwing an error you are simply handling it. – user2140261 Apr 03 '13 at 22:25
  • See my previous comment :) – Siddharth Rout Apr 03 '13 at 22:26
  • 4
    Ok, Well I accepted this as the answer but I much rather an explanation or a point in the right direction then a piece of code with no description on how or why it works. And when I asked about the error handling and why it works I simply got a response that it does work. This is not what this site is for, it is not to ask for an answer and get it but to learn. So for anyone in the future. The Collection variable only works on 2 conditions. One you associate a key with the values you are adding, and 2 ignore the errors that tell you when you are duplicating keys. – user2140261 Apr 04 '13 at 00:01
  • 1
    @user2140261: I just re-read the comments now. I see the comment was edited to `But the collection is still throwing an error you are simply handling it.` Had I seen the new comment then I would have definitely explained it. What you said in your last comment about about learning is very true :) Apologies for missing that comment. – Siddharth Rout Apr 04 '13 at 05:39
  • A collection item cannot have the same Key as another item. The values can be the same, but the Keys (second argument to Add method) must be unique. By using the value as the Key (I use `Cstr(CellVal)` b/c the Key must be a String data type), you are ensuring that only unique values are in the collection. You are correct that an error is thrown. The Add method fails due to the error, but the code happily continues trying to add more stuff to the collection bc the error is being ignored. – Dick Kusleika Apr 04 '13 at 13:01
  • this line return error `For Each itm In Col` *Type mismatch (Error 13)* – Rocketq Jul 10 '15 at 19:18