1

I have a list with 3 columns

for example

I want to delete any duplicate value with no shifting, the duplicated values can be both on the first column and on the second.

How can I do that?

I've tried something but it didn't work

Sub RemoveDuplicates()
Dim rng As Range
Dim x As Long
Dim lRow As Long
Dim i As Integer

Columns("B:C").Select
    Range("C1").Activate
    Selection.Replace What:="-", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="0", Replacement:="0", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
i = 1
x = 1
Do While Cells(i, 1).Value <> ""
    Cells(i, 4) = "=CONCATENATE(0,RC[-2])"
    i = i + 1
Loop
Do While Cells(x, 1).Value <> ""
    Cells(x, 5) = "=CONCATENATE(0,RC[-2])"
    x = x + 1
Loop
    Columns("D:E").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("B1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Columns("D:E").ClearContents
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

With ThisWorkbook.Sheets(1)
    lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    Set rng = ThisWorkbook.Sheets(1).Range("B2:C" & lRow)
End With

For x = rng.Cells.Count To 1 Step -1
    If WorksheetFunction.CountIf(rng, rng(x)) > 1 Then
        rng(x).ClearContents
    End If
Next x

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
2
  • The first thing coming to mind is using a loop to go through each row of each column, match() the value in the activecell, and if it matches, delete and move to next cell in the range. Commented Jan 19, 2017 at 13:20
  • you can add the Macro to the Toolbar excel-easy.com/vba/examples/add-a-macro-to-the-toolbar.html Commented Jan 24, 2017 at 22:28

1 Answer 1

1

Try this where your two columns are B and C. It loops through all of the data and uses the worksheet function COUNTIF to check if there is more than one occurrence of each value and clears the contents of the cell if there is a count of more than 1:

Sub RemoveDuplicates()

Dim rng As Range
Dim x as Long
Dim lRow as Long

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

With Thisworkbook.Sheets("SheetName")
    lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    Set rng = .Range("B2:C" & lRow)
End With

For x = rng.Cells.Count To 1 Step -1
    If WorksheetFunction.CountIf(rng, rng(x)) > 1 Then
        rng(x).ClearContents
    End If
Next x

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

thnx. its works great. but i have a problem, ive made an actives bottun with the command ant it works, but when i trie to make the same code as a macro command it didnt work and the debuger highlighted the row "With ThisWorkbook.Sheets("Sheet1")". the name of the seet is "Sheet1" so i dont understand whats the prob
That's odd, are you sure there aren't any leading/trailing spaces in the name, e.g. " Sheet1" or "Sheet1 "? I'm not sure what else the issue could be - you could always just use Thisworkbook.Sheets(1) if the worksheet is the first one in the workbook.
ive uploaded the whole code, maybe the problem is elsewhere
@bakman you might be able to use the CodeName instead With Sheet1 or maybe fully qualify it Application.Thisworkbook.Worksheets("Sheet1")
nothing works, when i use the code in an activex button it does works, so is there a way to add this button to excel so it will apper wen ever i open an excel file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.