• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Variables do not work in RemoveDuplicates Method|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Variables do not work in RemoveDuplicates Method|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVariables do not work in RemoveDupl…
sp_PrintTopic sp_TopicIcon
Variables do not work in RemoveDuplicates Method
Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
1
September 9, 2020 - 1:46 pm
sp_Permalink sp_Print
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.
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
September 9, 2020 - 2:14 pm
sp_Permalink sp_Print

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

Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
3
September 11, 2020 - 3:08 am
sp_Permalink sp_Print

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. 🙂

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
September 11, 2020 - 8:08 am
sp_Permalink sp_Print

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
5
September 11, 2020 - 8:47 am
sp_Permalink sp_Print

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

https://docs.microsoft.com/en-.....Duplicates

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

Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
6
September 12, 2020 - 10:32 am
sp_Permalink sp_Print

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.

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
7
September 12, 2020 - 10:48 am
sp_Permalink sp_Print

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

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Roslyn Finlayson
Guest(s) 11
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6210
Posts: 27236

 

Member Stats:
Guest Posters: 49
Members: 31888
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.