Macro to delete duplicate rows but leave column A

JadonR

Board Regular
Joined
Apr 15, 2009
Messages
154
Office Version
  1. 365
I'm using the following macro to delete duplicate rows (keeps the first value, then removes all duplicates after). However, I would like to keep column A untouched. So when the macro runs, the entire duplicate row except the cell in column A should be deleted. Is this possible?

Code:
Sub test()
    Dim r As Range, txt As String
    With CreateObject("Scripting.Dictionary")
Again:
        For Each r In Range("Q1", Range("Q" & Rows.Count).End(xlUp))
            If Not .exists(r.Value) Then
                .Add r.Value, Nothing
            Else
                txt = txt & "," & r.Address(0, 0)
                If Len(txt) > 245 Then
                    Range(Mid(txt, 2)).EntireRow.Delete
                    .RemoveAll
                    txt = Empty: GoTo Again
                End If
            End If
        Next
    End With
    If Len(txt) Then Range(Mid(txt, 2)).EntireRow.Delete
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Maybe try something like this...
Code:
Intersect(Range(Mid(txt, 2)).EntireRow, Columns("B:IV")).Delete

You realize that the data in column A will no longer be in the same rows as the rest of the data after the macro deletes.
 
Last edited:
Upvote 0
Yes I do realize that. I only have image buttons (for macros) in Col A and it re-arranges those buttons every time the macro deletes any rows under them.

This row appears twice in the macro:
Range(Mid(txt, 2)).EntireRow.Delete

I assume I need to replace that with your's in both places?
 
Upvote 0
I only have image buttons (for macros) in Col A and it re-arranges those buttons every time the macro deletes any rows under them.

You could also right-click on those buttons and select Format Control from the pop-up menu. On the Properties tab, select Don't move or size with cells.
 
Upvote 0
Thanks, your modification works perfectly. Good tip on setting them not to move or size with the cells. Even better, would be if they could stay in view (float), when I scroll down/up. The macro buttons go pretty much the whole distance down the left side, so expanding row 1 and freezing wouldn't really be an option.
 
Upvote 0
Even better, would be if they could stay in view (float), when I scroll down/up. The macro buttons go pretty much the whole distance down the left side.

You're welcome. I'm glad it worked.

Instead of buttons in column A, you could make a custom toolbar (right-click on any toolbar and select Customize then select New) and park it on the left side.

Create a custom toolbar
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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