VBA code to dynamically remove duplicates?

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I recorded a macro to remove duplicates from data in column A.

The data set was very simple - I had the number 1 in cells A1, and A2, respectively.

And the number 2 in cells A3, and A4, respectively.

However, this code only works on a fixed range. Does anyone know how to make it dynamic, please?

VBA Code:
Sub remove_duplicates()

    Columns("A:A").Select
    ActiveSheet.Range("$A$1:$A$4").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Just use the entire column
VBA Code:
    Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo
 
Upvote 0
Hi Fluff

Do you know how I can amend this if I have data in columns A-G with duplicates, but I want to delete duplicates from the rows based on data in column G, please?

VBA Code:
Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo
 
Upvote 0
I have data in columns A-G with duplicates, but I want to delete duplicates from the rows based on data in column G

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Columns("A:G").RemoveDuplicates Columns:=7, Header:=xlNo

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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