Creating a VBA to find and delete duplicate values within Excel workbook

Nikolova84

New Member
Joined
Jul 28, 2022
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I am searching a way to find and replace/delete duplicates of the particular list of data (lets say, positioned in column A, Sheet 1) in the entire Excel workbook.
What i mean is, if this is my data (in column A, bellow), is there a way to find all the duplicate values in Sheet 2, 3 et cetera and delete them, based on this list ?

1671284608900.png


Thank you in advance for you time and cooperation!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi there.

It's indicated in the first sentence that you'd want the data to be replaced/deleted. Can you clarify?

Assuming all data is to be deleted: Is the corresponding data to be deleted in the other sheets also in column A, or could it be in random columns? As for the deletion itself, would you want the cell where the data was found to be cleared, the cell deleted (in that case deleted and shifted up?), or would it be preferred that the entire row where that data is found to be deleted?
 
Upvote 0
Hi there.

It's indicated in the first sentence that you'd want the data to be replaced/deleted. Can you clarify?

Assuming all data is to be deleted: Is the corresponding data to be deleted in the other sheets also in column A, or could it be in random columns? As for the deletion itself, would you want the cell where the data was found to be cleared, the cell deleted (in that case deleted and shifted up?), or would it be preferred that the entire row where that data is found to be deleted?
Hello,

Thank you for your reply! :)

Concerning your first question, yes, i clarify that i want data to be deleted. Regarding the second question, the coresponding data to be deleted would not be only in column A, but rather random within the sheets. And about the third question, i would like the data in the cell to be just cleared and cell to remain blanc.

Thank you once again.
 
Upvote 0
Okay. Sounds like replacing the value with a blank would better suit your needs. Give the below a test in a normal module.

VBA Code:
Sub ClearList()

Dim wb As Workbook: Set wb = ThisWorkbook
'Declares sheet with data list
Dim ws As Worksheet: Set ws = wb.Sheets("Sheet1")
'Finds last row in ws (Sheet1)
Dim lrow As Long: lrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
'Sets data list range in column A of Sheet1 going to last row
Dim dlist As Range: Set dlist = ws.Range("A2:A" & lrow)

'loops through dlist
Dim c As Range, wkst As Worksheet
For Each c In dlist.Cells
    If Not c.Value = vbNullString Then
        'then loops through all worksheets except ws (i.e.; Sheet1)
        For Each wkst In wb.Worksheets
            If Not wkst.Name = ws.Name Then
                'Will replace found dlist item with a blank
                Dim rng As Range: Set rng = wkst.UsedRange
                With rng
                    .Replace What:=c.Value, Replacement:="", LookAt:=xlWhole
                End With
            End If
        Next wkst
    End If
Next c

End Sub
 
Upvote 0
Solution
Okay. Sounds like replacing the value with a blank would better suit your needs. Give the below a test in a normal module.

VBA Code:
Sub ClearList()

Dim wb As Workbook: Set wb = ThisWorkbook
'Declares sheet with data list
Dim ws As Worksheet: Set ws = wb.Sheets("Sheet1")
'Finds last row in ws (Sheet1)
Dim lrow As Long: lrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
'Sets data list range in column A of Sheet1 going to last row
Dim dlist As Range: Set dlist = ws.Range("A2:A" & lrow)

'loops through dlist
Dim c As Range, wkst As Worksheet
For Each c In dlist.Cells
    If Not c.Value = vbNullString Then
        'then loops through all worksheets except ws (i.e.; Sheet1)
        For Each wkst In wb.Worksheets
            If Not wkst.Name = ws.Name Then
                'Will replace found dlist item with a blank
                Dim rng As Range: Set rng = wkst.UsedRange
                With rng
                    .Replace What:=c.Value, Replacement:="", LookAt:=xlWhole
                End With
            End If
        Next wkst
    End If
Next c

End Sub
Thank you for your help, regarding my problem! I am going to try your code as soon as possible! ☺️ Wish you a wonderful day!
 
Upvote 0
Hello,

I tried your code and it works great. Just another thing, is it possible data in the sheet A, from which i made a comparison, to be formated in different color, when the value in the cell is unique and there aren't any duplicates in other sheets? For example, if cell A14 is unique value and doesn't have duplicates, i want this cell to be formated and somehow distinguished.

Thanks in advance for your help!
 
Upvote 0
Hello,

I tried your code and it works great. Just another thing, is it possible data in the sheet A, from which i made a comparison, to be formated in different color, when the value in the cell is unique and there aren't any duplicates in other sheets? For example, if cell A14 is unique value and doesn't have duplicates, i want this cell to be formated and somehow distinguished.

Thanks in advance for your help!
Another option would be to just delete duplicate values in column A of Sheet 1 also.
 
Upvote 0

Forum statistics

Threads
1,214,797
Messages
6,121,629
Members
449,041
Latest member
Postman24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top