+ Reply to Thread
Results 1 to 17 of 17

Delete duplicate row base on 2 cells.(condition)

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Delete duplicate row base on 2 cells.(condition)

    Hi,

    I have searched over the net but I can't find the one that correspond to what I need.

    I will need to delete duplicate row base on the condition of Column B and C.

    The duplicate items is in column C, but before delete, check column B, if same, then delete, if deifferent , then keep both.

    Here is the sample of th worksheet, and in the 2nd tab, it has the result that should be.

    Duplicate Delete.xlsx

    Thanks for help.

  2. #2
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Delete duplicate row base on 2 cells.(condition)

    Try this to see if this works as per your requirement.
    Please Login or Register  to view this content.
    Last edited by sktneer; 10-02-2014 at 12:47 AM.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete duplicate row base on 2 cells.(condition)

    Hi, sktneer,

    i is dimmed as Variant, no need to select a Cell prior to taking comparision (except for slowing the code down), and an alternative to deletion may be
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Delete duplicate row base on 2 cells.(condition)

    Oh yes. Thanks Holger for pointing this out.
    Actually I wrote this line while debugging the code and forgot to remove it. I have edited the code now. Thanks once again.

  5. #5
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Delete duplicate row base on 2 cells.(condition)

    Hi,

    The problem with this code is the data has to be sorted. Is there a way to make it work if the data is not sorted? See example:

    Duplicate Delete.xlsm

  6. #6
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Delete duplicate row base on 2 cells.(condition)

    Please find the attached sheet and click on the Green Button on Test Sheet to see if you get the desired output as you have shown on Sheet2.
    Attached Files Attached Files

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete duplicate row base on 2 cells.(condition)

    Hi, sktneer,

    Please Login or Register  to view this content.
    dims i and lr as Variant and only lc as long - same as above.

    @calvinle:
    add an additional column and concatenate the contents to make it the items comparible:
    Please Login or Register  to view this content.
    No need to sort the data prior to running a macro.

    Ciao,
    Holger

  8. #8
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Delete duplicate row base on 2 cells.(condition)

    Quote Originally Posted by HaHoBe View Post
    Hi, sktneer,

    Please Login or Register  to view this content.
    dims i and lr as Variant and only lc as long - same as above.
    Ciao,
    Holger
    Why dim i, lr as Variant and only lc as long? Any logic behind it?
    Do you think declaring a variable as a variant, specially when you know that what type of data it will be holding during the code, is a good practice?

    No need to sort the data prior to running a macro.
    Is there any harm if someone sorts the data if required in order to deleting undesired rows and specially when you don't need to restore the data sequence as it was before running the macro?

  9. #9
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Delete duplicate row base on 2 cells.(condition)

    I think I got your point.
    In Dim i, lr, lc as long VBA will treat lc as a long and i, lr as variant. Right?

    While the correct way to declare these variables as long will be as follows.......
    Dim i As Long, lr As Long, lc As Long. Correct?

    Thanks once again for your guidance and sharing this tip.

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete duplicate row base on 2 cells.(condition)

    Hi, sktneer,

    the logic that VBA needs each Variable to be dimmed on itīs own with Variant being the type which may be converted into any other type.

    Please Login or Register  to view this content.
    As I do have severe problens with typos I rely on Option Explicit and some thoughts on what the Variable is assumed to hold (using Doubles for Row or Column Counters always looks odd to me).

    No harm, and nowadays everybody enjoys lots of RAM - no need to count the bytes anymore.

    Ciao,
    Holger

  11. #11
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Delete duplicate row base on 2 cells.(condition)

    Point already taken.
    Holger! You are a genius.
    I really appreciate you for pointing out these siliy mistakes otherwise no one takes interest in sharing his knowledge with others.
    Thanks once again.

  12. #12
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Delete duplicate row base on 2 cells.(condition)

    HI HaHoBe,

    What is the column D for? I removed data from this sample file, but there are data in column D. Doing so will delete my column D..

    Thanks.
    Last edited by calvinle; 10-02-2014 at 03:43 PM.

  13. #13
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Delete duplicate row base on 2 cells.(condition)

    I got it..! Thanks!

  14. #14
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Delete duplicate row base on 2 cells.(condition)

    Hi,

    I did not know that the order of the data has an impact to the code.
    I have tried to replace the column id and worksheet to match the order of my data, but it doesn't work.
    Anything wrong in my code?
    It does not give the result in result sheet.

    Duplicate Delete.xlsm

    Please help.

    Thanks

  15. #15
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete duplicate row base on 2 cells.(condition)

    Hi, calvinle,

    you would need to refer to the new column throughout the code and not compare the values of the newly concateneted column to Column D.

    Please Login or Register  to view this content.
    Ciao,
    Holger

  16. #16
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Delete duplicate row base on 2 cells.(condition)

    HI HaHoBe,

    Greatly appreciate your help. I was able to make it look like my result, however, I forgot that there is possibility that the column C appears to have a different data, but same data in column I. So, I don't want to delete it since it's from another product.

    Can you help me once more to have it compare to Column C as well?
    Like, compare column C, D, and I, instead of just D and I.

    Duplicate Delete.xlsm

    Thank you very much!

  17. #17
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete duplicate row base on 2 cells.(condition)

    Hi, calvinle,

    I donīt like to answer to threads that are being marked Solved - they either are (then no questions should be open) or arenīt (then they should not be marked Solved).

    All you would need to do is insert a third column in the line for evaluate and maybe change the order in which the columns appear.

    Please Login or Register  to view this content.
    could be changed to read
    Please Login or Register  to view this content.
    Ciao,
    Holger

+ 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. Delete Duplicate Cells
    By Fredbugatti in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-19-2014, 02:44 PM
  2. [SOLVED] Please I need a macro to delete duplicate cells in a several selected cells in Excel?
    By amkoloui in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 02-15-2014, 02:17 PM
  3. Delete duplicate cells and more....
    By Elegidos in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 10-09-2009, 04:56 AM
  4. [SOLVED] How do I delete duplicate cells?
    By AYANG in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-27-2006, 01:30 AM
  5. would like to delete duplicate cells
    By jd in forum Excel General
    Replies: 3
    Last Post: 07-01-2005, 02:05 PM

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