+ Reply to Thread
Results 1 to 11 of 11

How to delete the ENTIRE row if only one cell is duplicated?

  1. #1
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    How to delete the ENTIRE row if only one cell is duplicated?

    Friends,
    I have a file with 238 rows and 10 columns (see file attached)
    Column G include phone numbers, some phone numbers are duplicated.

    How can I use a macro (I must do it with a macro) to delete the ENTIRE row if the value in column G is the same in other row?

    For example: look at rows 2 and 3 : the value in column G2 and G3 is the same, therefor delete row 3

    (and if the value in G4 was also the same, then delete also row 4, and so on..)


    Attached Files Attached Files
    Last edited by sami770; 06-15-2011 at 12:05 PM.

  2. #2
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    Guys, can anyone help? I really need this urgently

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    sami770,

    Give this a try:
    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    You can use a COUNTIF() formula to test for duplication. In an empty column I would enter this formula and copy down:

    =COUNTIF($G$1:$G2,$G2)

    Then I would turn on the Data > Filter > Autofilter and do a custom filter on column K for greater than 1.

    I would then delete all visible rows, turn off the AutoFilter and clear the added column.

    Here's a macro that does it that way:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    Hi tigeravatar, I just ran your code and it worked on the example workbook but what I don't get is where you are telling it to compare phone numbers? Is that what CountIf does? I don't mean to jump in on this and ask questions in threads that aren't mine but I was working on this using an array but did not get the desired results (yet).
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    Thanks Jerry, you answered my question.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    Glad to help, Mordred.

    Tiger, whenever we're doing the same test over and over again on every row, much more efficient to do the tests all at once and delete the rows all at once using an AutoFilter.

  8. #8
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    @Mordred, hehe, your question was answered before I even read it

    @JBeaucaire, yeah, my macro isn't particularly efficient because it tests each cell individually. I took the shortcut because there was only 238 rows which isn't very many. I should strive to maintain efficiency though. Shortcuts are just so attractive sometimes, hehe

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    Heh, understood. I'm not swayed by fewer lines of code, my eye is always on the total number of executed commands. Your loop is only 3 lines of code, but it represents 238 commands. That's why I opt for a few more lines of code to get it down to a grand total of 8 commands, even if his code was 10000 rows, still only 8 commands.

    I'm a huge fan of the AutoFilter.

  10. #10
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    Tiger - you macro works perfect!
    MANY THANKS

    Quote Originally Posted by tigeravatar View Post
    sami770,

    Give this a try:
    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar

  11. #11
    Valued Forum Contributor scaffdog845's Avatar
    Join Date
    02-01-2008
    Location
    Aston, PA. USA.
    MS-Off Ver
    Microsoft 365
    Posts
    373

    Re: How to delete the ENTIRE row if only one cell is duplicated?

    A quick Google search with the site:excelforum.com switch at the end provides yet another answer to a question I had. Thanks guys! And yes... I know it's a old thread.
    Click here to read the Forum Rules
    Whatever it is in life you decide to go after, go after with great ferocity.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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