Help with understanding a remove duplicate macro

Taiba

New Member
Joined
Jan 9, 2018
Messages
2
Hello,
I'm new to VBA and had searched through the forums and found the following macro than can be used to delete duplicate rows where the range is dynamic. would you explain to me how does this macro work? what is the meaning of this in particular (strRange = "$A$1:" & ActiveCell.SpecialCells(xlLastCell).Address)


strRange = "$A$1:" & ActiveCell.SpecialCells(xlLastCell).Address
ActiveSheet.Range(strRange).RemoveDuplicates

Thanks:eeek:
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Welcome to the Board!

The first line is just building a string to represent the range you want to apply this to.
"ActiveCell.SpecialCells(xlLastCell)" is the same as pressing CTRL-END on your Excel sheet. That will jump to the last cell with data (or held data).
The ".Address" part just returns the address of that cell.
So if that cell was Z100, the value of strRange would be: "$A$1:$Z$100"

Note that ranges in VBA are denoted like this:
Range("A1")
Range("$A1:$M100)

etc

So, all we are doing is turning that strange into a real range, and then applying the "Remove Duplicates" functionality to it.
Note that the newer version of Excel have "Remove Duplicates" on the Data menu.

Also note that you can get the VBA code for some basic actions you can perform in Access by turning on the Macro Recorder, and recording yourself performing actions, like removing duplicates. This is a good tool for beginners, and for experts alike, as we do not necessarily need to remember the exact structure of certain VBA commands if we can get that part by using the Macro Recorder.

I hope that helps. If you have other questions about this code, please let us know below.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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