VBA Script identify duplicates and clear contents specific cells

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
Hello group,
I was looking for some help making a script that will identify the last row used, go through column E to find duplicates, once it finds duplicated it will clear contents of cells D:E and G:L on the row leaving only the first instance of the number intact. I do not want to delete the cells as it will change the alignment of other cells and thus ruin the spreadsheet.

There may be only 2 lines or 4 or 5 lines duplicate but the duplicated will be grouped together.

Thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I THINK this should work. Might need a little tinkering to get it right. Definitely test it on a copy of your data, not the original dataset to avoid... ya know... wrecking your work.

VBA Code:
Dim lRow as Long
Dim ws   as Worksheet

Set ws = Application.Thisworkbook.Worksheets("YourSheetName")

lRow = ws.cells(Rows.Count,1).End(xlUp).Row

For lRow to 2 Step -1
    If ws.Cells(lRow, 5) = ws.cells(lRow -1, 5) Then
        'ws.Rows(""" & lRow & ":" & lRow & """).Select 'Code to delete full row
        'Selection.Delete Shift:=xlUp 
        ws.Range("D" & lRow & ":E" & lRow)  = ""
        ws.Range("G" & lRow & ":L" & lRow) = ""
    End if
Next lRow
 
Upvote 0
Here's another way to consider:
VBA Code:
Sub FindDupsAndClear()
Dim Lrw As Long, R As Range, CalcState, d As Object, c As Range
Set R = Range("E1:E" & Cells(Rows.Count, "E").End(xlUp).Row)
CalcState = Application.Calculation
With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
End With
Set d = CreateObject("Scripting.dictionary")
For Each c In R
    If d.exists(c.Value) Then
        With c
            .Value = ""
            .Offset(0, -1).Value = ""
            .Offset(0, 2).Resize(1, 6).Value = ""
        End With
    Else
        d.Add c.Value, d.Count + 1
    End If
Next c
With Application
    .Calculation = CalcState
    .ScreenUpdating = True
End With
End Sub
 
Upvote 0
Another way to consider that does not require checking every row individually.

VBA Code:
Sub Clear_Dupes()
  Application.ScreenUpdating = False
  Cells(2, Columns.Count).Formula = "=MATCH(E2,E:E,0)<>ROW(E2)"
  With Range("E1", Range("E" & Rows.Count).End(xlUp))
    .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Cells(1, Columns.Count).Resize(2), Unique:=False
    If .SpecialCells(xlVisible).Count > 1 Then Intersect(.EntireRow, Range("D:E, G:L"), Rows("2:" & Rows.Count)).ClearContents
  End With
  ActiveSheet.ShowAllData
  Cells(2, Columns.Count).ClearContents
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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