+ Reply to Thread
Results 1 to 18 of 18

Delete rows using duplicates from one column macro code issue

  1. #1
    Forum Contributor
    Join Date
    03-01-2019
    Location
    New York, New York
    MS-Off Ver
    Office 2016 for Mac
    Posts
    125

    Delete rows using duplicates from one column macro code issue

    Hi, I am currently trying to write a code to select all data and then delete all rows based on duplicates from a single column. I want to delete rows based on duplicates in columns A, J, E, and K. I typically do this by hand one column at a time using the Remove Duplicates tool in Data tab. However, I am trying to write a code since this is a repetitive process. The code as it currently exists seems to delete duplicates using column F four times rather than doing 4 different columns (A, J, E and K).

    Please Login or Register  to view this content.

  2. #2
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,743

    Re: Delete rows using duplicates from one column macro code issue

    Try like this:

    Please Login or Register  to view this content.
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  3. #3
    Forum Contributor
    Join Date
    03-01-2019
    Location
    New York, New York
    MS-Off Ver
    Office 2016 for Mac
    Posts
    125

    Re: Delete rows using duplicates from one column macro code issue

    Thank you for the response. From my understanding, this code will look at every column from A to Q and then remove duplicates based on every column. Is that correct? For my task, I will only want to check columns A, J, E and K for duplicates and no others.

  4. #4
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,743

    Re: Delete rows using duplicates from one column macro code issue

    Quote Originally Posted by Ianmacros View Post
    Thank you for the response. From my understanding, this code will look at every column from A to Q and then remove duplicates based on every column. Is that correct? For my task, I will only want to check columns A, J, E and K for duplicates and no others.
    Sorry, I've sent it before adapt for specification :-)

    Put instead of iCol loop:

    Please Login or Register  to view this content.
    Last edited by KOKOSEK; 03-19-2019 at 12:21 PM.

  5. #5
    Forum Contributor
    Join Date
    03-01-2019
    Location
    New York, New York
    MS-Off Ver
    Office 2016 for Mac
    Posts
    125

    Re: Delete rows using duplicates from one column macro code issue

    No problem!

    When I test the code, it endlessly loops through columns A, J, E and K selecting them but not deleting the duplicates. Not sure why.

  6. #6
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,743

    Re: Delete rows using duplicates from one column macro code issue

    Hmm it works for me.

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    03-01-2019
    Location
    New York, New York
    MS-Off Ver
    Office 2016 for Mac
    Posts
    125

    Re: Delete rows using duplicates from one column macro code issue

    My thought exactly. I had to add in one line of code, which I think was just accidentally left off. The 'Select Range part of the original code. Seems to work now. Appreciate the help!

    Please Login or Register  to view this content.

  8. #8
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,743

    Re: Delete rows using duplicates from one column macro code issue

    You welcome.
    If you happy with solution please use Thread tools and mark thread as SOLVED.

  9. #9
    Forum Contributor
    Join Date
    03-01-2019
    Location
    New York, New York
    MS-Off Ver
    Office 2016 for Mac
    Posts
    125

    Re: Delete rows using duplicates from one column macro code issue

    OK so upon a closer look it is very close but needs one more small tweak. Right now, it is just deleting the cell with the duplicate. I want the entire row to be deleted if a duplicate cell is found.

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Delete rows using duplicates from one column macro code issue

    Odd Ianmacros, your original code does exactly what you want it to do when I run it on my end. After running it, do you still have duplicates in A E J or K?
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  11. #11
    Forum Contributor
    Join Date
    03-01-2019
    Location
    New York, New York
    MS-Off Ver
    Office 2016 for Mac
    Posts
    125

    Re: Delete rows using duplicates from one column macro code issue

    Arkadi, the duplicate values are deleted but the rows in which they exist remain.

  12. #12
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Delete rows using duplicates from one column macro code issue

    I meant your FIRST post, not the replies. Yes I know the reply would not delete the whole row, but your first code should.

  13. #13
    Forum Contributor
    Join Date
    03-01-2019
    Location
    New York, New York
    MS-Off Ver
    Office 2016 for Mac
    Posts
    125

    Re: Delete rows using duplicates from one column macro code issue

    Oh sorry I understand. The first code brought up delete duplicates window but it looks like it highlights column K and loops four times rather than for the 4 different columns I thought I specified (A, J, E, K). However, in the data, it looks like it does what it should do. Any thoughts?

  14. #14
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Delete rows using duplicates from one column macro code issue

    Not sure why it looks like column K would look highlighted, in theory the whole range should be but yes the last column would be K maybe that's why? Or are you stepping through the code and it does it from the start?

    Either way there is no need to select the range before removing duplicates, you are specifying the applicable range in the removeduplicates commands.

    On my end the results with your code are as expected, and the whole range gets selected, and I don't see any column looking highlighted at all.
    Last edited by Arkadi; 03-19-2019 at 01:44 PM.

  15. #15
    Forum Contributor
    Join Date
    03-01-2019
    Location
    New York, New York
    MS-Off Ver
    Office 2016 for Mac
    Posts
    125

    Re: Delete rows using duplicates from one column macro code issue

    I think I am not being clear here. The entire data range is selected. Correct. However, when the remove duplicates dialog pops up, it does so 4 times. I deleted the last 3 rows (see below) and even then it pops up 4 times when just checking column A.
    Please Login or Register  to view this content.

  16. #16
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Delete rows using duplicates from one column macro code issue

    Weird, I never even get a dialog popping up when removing duplicates via code.
    Any chance you have a worksheet_change event running that would trigger this macro to run multiple times?

    If you could attach the file with all existing code (some sample data would be handy but not so important in this case), maybe we can see more of what is going on.

  17. #17
    Forum Contributor
    Join Date
    03-01-2019
    Location
    New York, New York
    MS-Off Ver
    Office 2016 for Mac
    Posts
    125

    Re: Delete rows using duplicates from one column macro code issue

    Arkadi, I think the code I am using is just not equivalent to what I would do by hand. When I replicate what the code does by hand, it's different than what I actually do by hand. I'll just keep doing this manually. I don't think my understanding of VBA is strong enough to code this even with your help. Thanks for your patience and help!

  18. #18
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Delete rows using duplicates from one column macro code issue

    I believe if I understood correctly, the code should be doing the same as you are doing manually... i've automated the same in a few programs I have. However, it is your decision. If you decide to pursue it further, feel free to ask questions or provide a sample workbook with input and expected output.

    We are always happy to help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro that can delete rows where duplicates found and replace with average value
    By vbronton in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-13-2018, 01:29 PM
  2. Replies: 7
    Last Post: 03-13-2015, 05:34 PM
  3. Macro To Find Rows With Duplicates, Compare Cells, And Delete Rows. - Excel
    By Kwame001 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-26-2013, 10:41 AM
  4. How to efficiently delete entire rows based on duplicates in one column
    By HughManatee in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-16-2013, 06:53 PM
  5. Replies: 8
    Last Post: 08-08-2013, 08:45 AM
  6. [SOLVED] Macro to count unique values in a column, enter it in next column, then delete duplicates
    By pmorisse in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-25-2013, 03:27 PM
  7. Replies: 13
    Last Post: 08-23-2011, 11:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1