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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,837
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
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
 

excelrule

New Member
Joined
Aug 20, 2012
Messages
11
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.
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
VBA Code:
 Range("A" & i & ":" & "EH" & i).ClearContents
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,882
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 

excelrule

New Member
Joined
Aug 20, 2012
Messages
11
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!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,882
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,163
Messages
5,640,511
Members
417,148
Latest member
pe3087te

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
Top