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
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. 🙂
no worries Bob - no file attached though. Did you click on the orange 'Start Upload' button?
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
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.
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