Forum

Variables do not wo...
 
Notifications
Clear all

Variables do not work in RemoveDuplicates Method

7 Posts
2 Users
0 Reactions
74 Views
(@bobkap)
Posts: 17
Eminent Member
Topic starter
 
If I use variables for the "Cells" the line bombs out the macro.  If I use numbers, it works fine.
Range(Cells(1, 1), Cells(200, finalcol)).RemoveDuplicates Columns:=Array(email, courseyear), Header:=xlYes
 
I must use variables for the usual reasons.  Any help would be greatly appreciated.
 
Thanks.
 
Posted : 09/09/2020 11:46 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Bob,

This works fine for me

Sub xxxx()

Dim finalcol As Long
finalcol = 2

Range(Cells(1, 1), Cells(20, finalcol)).Select

End Sub

so not sure what the problem is with your code. Please attach your file so I can check it.

regards

Phil

 
Posted : 10/09/2020 12:14 am
(@bobkap)
Posts: 17
Eminent Member
Topic starter
 

Hi Phil.  Sorry for the delayed reply, I was stuck in meetings (UGH) the last few days.

Here's the file.  Here's the line of code that's giving me issues.  Instead of the number 200, I want to use the variable "email" there as I will never know each time I run this report whether I'll have 100 rows of data or 5,000 rows.  

Range(Cells(1, 1), Cells(200, finalcol)).RemoveDuplicates Columns:=Array(email, courseyear), Header:=xlYes

PS No laughing at my code please.  I'm sure it leaves a lot to be desired. 🙂

 
Posted : 11/09/2020 1:08 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

no worries Bob - no file attached though.  Did you click on the orange 'Start Upload' button?

 
Posted : 11/09/2020 6:08 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

That said, the Columns array you supply to RemoveDuplicates is a list of the column numbers that contain duplicates

https://docs.microsoft.com/en-us/office/vba/api/Excel.Range.RemoveDuplicates

so if email is 200 then Excel is trying to remove duplicates from column GR.  Is that what you are intending to do?

Regards

Phil

 
Posted : 11/09/2020 6:47 pm
(@bobkap)
Posts: 17
Eminent Member
Topic starter
 

No.  If column C and H are duplicates to other rows of data, I need to remove one of them.

So, in this case, if "email" and "year" are the same, one row has to go.  But, if I use the email variable, the macro bombs out.

 
Posted : 12/09/2020 8:32 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Bob,

I don't think you are using RemoveDuplicates correctly- did you read the link i posted?

RemoveDuplicates removes duplicates from a range, it doesn't compare two ranges and remove one of them.

Please attach your file so I can see what it is you are trying to do.

Regards

Phil

 
Posted : 12/09/2020 8:48 pm
Share: