VBA Clear Duplicate Rows in Range

excelrule

New Member
Joined
Aug 20, 2012
Messages
11
Hi Experts,

I need to clear (not delete) rows in column range A:EH based on in row duplicates in column A. I use this formula to find the duplicate column A values =COUNTIF($A$1:A1,A1)>1 but I need to clear the contents of the duplicate rows in column range A to EH.

I found the RemoveDuplicates function and the following code which completes the correct selection but deletes the range rather than clearing it.

VBA Code:
Sub Remove_Duplicates()

  Range("A1:EH99").RemoveDuplicates Columns:=1, Header:=xlYes

End Sub

Is anyone aware of a function that will do the same but clear the rows instead of deleting them?

Thanks if you can help!

regards

Mark
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this:
VBA Code:
Sub ClearDuplicates()
 Dim k As Long, Lr As Long
 Lr = Cells(Rows.Count, 1).End(xlUp).Row

 For i = 1 To Lr
  k = Application.WorksheetFunction.CountIf(Range("A1:A" & i), Range("A" & i))
   If k > 1 Then
      Range("A" & i).EntireRow.ClearContents
   End If
 Next i
End Sub
 
Upvote 0
Hi maabadi,

Thanks for your reply. The code works great but I need to restrict it to column range A through EH.

I've tried to modify the range statement but can't seem to get that to work. Here's what I've tried.

VBA Code:
Sub ClearDuplicates()
 Dim k As Long, Lr As Long
 Lr = Cells(Rows.Count, 1).End(xlUp).Row

 For i = 1 To Lr
  k = Application.WorksheetFunction.CountIf(Range("A1:A" & i), Range("A" & i))
   If k > 1 Then
      Range("A:EH" & i).ClearContents
   End If
 Next i
End Sub

Do you know if it's possible to restrict the columns deleted in the range statement? I'm not a VBA expert as you can tell!

Thanks again.
 
Upvote 0
Try
VBA Code:
Sub ClearDuplicates()
 Dim k As Long, Lr As Long
 Lr = Cells(Rows.Count, 1).End(xlUp).Row

 For i = 1 To Lr
  k = Application.WorksheetFunction.CountIf(Range("A1:A" & i), Range("A" & i))
   If k > 1 Then
      Range("A" & i).Resize(, 138).ClearContents
   End If
 Next i
End Sub
 
Upvote 0
Thanks to everyone that's helped with this, I now have a fully functioning script to process my data. I really appreciate your input.

@maabadi thanks for your original code that got things running.
@mohadin and @Fluff , both of your tweaks worked!
 
Upvote 0

Forum statistics

Threads
1,213,539
Messages
6,114,221
Members
448,554
Latest member
Gleisner2

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