0
votes

I'm trying to copy and paste non duplicates values from one sheet to another sheet in excel. I need to copy and paste only unique values from formula column. I tried below code from stack exchange but it is working only on non formula columns. How do i copy paste non duplicate values only from formula column?

 Sub CopyUnique()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Main")
    Set s2 = Sheets("Count")
    s1.Range("B:B").Copy s2.Range("A1")
    s2.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
2

2 Answers

3
votes

Try to paste it as values:

Sub CopyUnique()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Main")
    Set s2 = Sheets("Count")
    s1.Range("B:B").Copy 
    s2.Range("A1").PasteSpecial xlPasteValues
    s2.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
0
votes

if you want to consider non formula values form worksheet s1 and paste their unique values in worksheet s2, then use this:

Sub CopyUnique()
    Dim s1 As Worksheet, s2 As Worksheet

    Set s1 = Sheets("Main")
    Set s2 = Sheets("Count")

    s1.Range("B:B").SpecialCells(xlCellTypeConstants).Copy
    With s2
        .Range("A1").PasteSpecial xlPasteValues
        .Range("A1", .Cells(.Rows.count, 1).End(xlUp)).RemoveDuplicates Columns:=1, Header:=xlNo
    End With
End Sub