VBA - Deduplicate a column without exact references

rachel06

Board Regular
Joined
Feb 3, 2016
Messages
126
Office Version
  1. 365
Platform
  1. Windows
Hi!

This is probably another easy one, but again, nothing I'm searching for is helping.

I want to remove duplicates from a column but I don't want it to have exact cell references. It's going to be a different number of rows for each dataset each time.

Everything I've tried either slows the macro way down, completely crashes excel, or crashes the macro.

This little bit is part of a larger code. My data starts in cell E1:

Code:
Columns("E:E").Select
    ActiveSheet.Range("$E$2:$E$28605").RemoveDuplicates Columns:=1, Header:=xlNo

I know this is super simple and super basic and I feel so silly that I can't figure it out. Hoping there's an easy fix to update!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Is there a reason not to use the Remove Duplicates tool on the Data tab?
1718637785864.png


You can find the last row of data like this:
VBA Code:
Dim lRow As Long
lRow = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("E2:E" & lRow).RemoveDuplicates Columns:=1, Header:=xlNo
 
Upvote 0
No need to select the column first.
Try this method that will dynamically find the last row in column E with data, and only apply it on that:
VBA Code:
'   Find last row in column E with data
    Dim lr As Long
    lr = Cells(Rows.Count, "E").End(xlUp).Row
   
'   Remove duplicates from column E
    Range("E2:E" & lr).RemoveDuplicates Columns:=1, Header:=xlNo
 
Upvote 1
Solution
dreid1011,

Have a closer look at this line - you have some typos in it:
Excel Formula:
lRow = ActiveSheet.Range("E2" & Rows.Count).End(xlUp).Row
 
Upvote 0
Is there a reason not to use the Remove Duplicates tool on the Data tab?
View attachment 112792

You can find the last row of data like this:
VBA Code:
Dim lRow As Long
lRow = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("E2:E" & lRow).RemoveDuplicates Columns:=1, Header:=xlNo
Yeah, it's part of a larger code. This step is right in the middle of it so I have things it needs to do after :)
 
Upvote 0
No need to select the column first.
Try this method that will dynamically find the last row in column E with data, and only apply it on that:
VBA Code:
'   Find last row in column E with data
    Dim lr As Long
    lr = Cells(Rows.Count, "E").End(xlUp).Row
  
'   Remove duplicates from column E
    Range("E2:E" & lr).RemoveDuplicates Columns:=1, Header:=xlNo
This seems to work perfectly. Thanks so much!!!
 
Upvote 0
dreid1011,

Have a closer look at this line - you have some typos in it:
Excel Formula:
lRow = ActiveSheet.Range("E2" & Rows.Count).End(xlUp).Row
Fixed it already, thanks.
 
Upvote 0
Fixed it already, thanks.
Not quite. Try it for yourself, you will see that you get an error (always a good idea to test your code first before posting it!).
You are setting it to be an invalid row number.
VBA Code:
Range("E2" & Rows.Count)
should be:
VBA Code:
Range("E" & Rows.Count)

Otherwise, you are starting with row number 21048576, which is not a valid row number on an Excel sheet!
 
Upvote 0
Not quite. Try it for yourself, you will see that you get an error (always a good idea to test your code first before posting it!).
You are setting it to be an invalid row number.
VBA Code:
Range("E2" & Rows.Count)
should be:
VBA Code:
Range("E" & Rows.Count)

Otherwise, you are starting with row number 21048576, which is not a valid row number on an Excel sheet!
I made the edit right after you copied me to point it out.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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