+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Thread: Finding duplicates in a column, prompt user then delete the current one

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Finding duplicates in a column, prompt user then delete the current one

    Hi there,

    I would like to have few lines to code to let Excel first find duplicates against column 1 when users enter new record. If a duplicate is found, then a pop-up window saying duplicate is found or something like that and then have Excel delete the current line so that we don't have to manually delete it by right-clicking then hit "Delete".

    The list goes on forever so I thought I should use a macro in worksheet change event, but i don' tknow how to get started.

    Is this possible?

    Please see the attached file.

    Thank you
    Attached Files Attached Files
    Last edited by Lifeseeker; 01-23-2012 at 07:24 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Finding duplicates in a column, prompt user then delete the current one

    I have this so far, which is referenced from someone else, but it deletes the entire column up to the header column. Is there any way that I just have the current row deleted as soon as it detects a duplicates?

    Dim Bottom_Row As Long, lloop As Long
         
        Application.ScreenUpdating = False
         
         'Delete duplicates in current column
        Bottom_Row = Range("A65526").End(xlUp).Row
         
        For lloop = Bottom_Row To 2 Step -1
            If Trim(ActiveSheet.Cells(lloop, 1).Value) = Trim(ActiveSheet.Cells(lloop, 1)) Then
                ActiveSheet.Cells(lloop, 1).EntireRow.Delete
            End If
        Next lloop
         
        Application.ScreenUpdating = True
    End Sub

  3. #3
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Finding duplicates in a column, prompt user then delete the current one

    How about something like this?

    This just deletes the cell contents as your file is empty and I am not sure if you want the entire row deleted or entire column deleted.

    Option Explicit
    Sub d()
        Dim Mbox As String
        Dim LR As Long, i As Long, found As Range
        Application.ScreenUpdating = False
         On Error Resume Next
            LR = Cells(Rows.Count, 1).End(xlUp).Row
        For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
            Set found = Range("a:a").Find(what:=Cells(i, "b"), LookIn:=xlValues, LookAt:=xlWhole)
          If Not found Is Nothing Then
                Mbox = MsgBox("Duplicate Found, do you want to delete duplicate", vbYesNo, "Duplicate")
            If Mbox = vbYes Then
              Cells(i, "b").EntireRow.Delete
            Else
            End If
          End If
        Next i
        
        Set found = Nothing
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by JapanDave; 01-20-2012 at 09:17 PM.
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  4. #4
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Finding duplicates in a column, prompt user then delete the current one

    Hi,

    great.

    Actually, I would like the entire row to clear the contents please.

    So something like range (cells 1) to ( cells 60) . Value = ""

    possible?

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Finding duplicates in a column, prompt user then delete the current one

    Clear cell contents if the next value you enter in col a has a duplicate.

    Only checking column a please.

  6. #6
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Finding duplicates in a column, prompt user then delete the current one

    Quote Originally Posted by Lifeseeker View Post
    Clear cell contents if the next value you enter in col a has a duplicate.
    I had already modified the code to delete the entire row. You can have it delete from cells 1- 60 , but that will leave a blank row?

    I don't understand what you are saying in this sentence?
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  7. #7
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Finding duplicates in a column, prompt user then delete the current one

    oh I see..

    hmm maybe deleting the entire row is a better option. I will check it tomorrow.

    Thank you

  8. #8
    Valued Forum Contributor
    Join Date
    02-20-2007
    Location
    South Africa
    MS-Off Ver
    2007
    Posts
    488

    Re: Finding duplicates in a column, prompt user then delete the current one

    @ JapanDave,

    Your Code works Fine! I tried to tweak it to only Clear the Range found with duplicates, but it does'nt work?
    Please consider:

    Be polite. Thank those who have helped you. Click the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  9. #9
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Finding duplicates in a column, prompt user then delete the current one

    Hey Winon,

    Try this and see if it works for you?

    Option Explicit
    Sub d()
        Dim Mbox As String
        Dim LR As Long, i As Long, found As Range
        Application.ScreenUpdating = False
         On Error Resume Next
            LR = Cells(Rows.Count, 1).End(xlUp).Row
        For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
            Set found = Range("a:a").Find(what:=Cells(i, "b"), LookIn:=xlValues, LookAt:=xlWhole)
          If Not found Is Nothing Then
                Mbox = MsgBox("Duplicate Found, do you want to delete entire row contents?", vbYesNo, "Duplicate")
            If Mbox = vbYes Then
              Rows(i).ClearContents
            Else
              Mbox = vbNo
            Resume Next
            End If
          End If
        Next i
        
        Set found = Nothing
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  10. #10
    Valued Forum Contributor
    Join Date
    02-20-2007
    Location
    South Africa
    MS-Off Ver
    2007
    Posts
    488

    Re: Finding duplicates in a column, prompt user then delete the current one

    @ JapanDave,

    Thank you very much for your time and effort. The code Clears the whole Row though, while I am trying to get it to clear only the range- Colums A through to Column i. This has been bugging me for a long time now.
    Please consider:

    Be polite. Thank those who have helped you. Click the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  11. #11
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Finding duplicates in a column, prompt user then delete the current one

    I am not sure I understand.

    Eg, if you find a duplicate from what column to what column in what row do you want to clear?
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  12. #12
    Valued Forum Contributor
    Join Date
    02-20-2007
    Location
    South Africa
    MS-Off Ver
    2007
    Posts
    488

    Re: Finding duplicates in a column, prompt user then delete the current one

    @ DaveJapan,

    Perhaps the notes I have made in your WB attached, will clear up the misunderstanding.

    Thank you for your patience!
    Attached Files Attached Files

  13. #13
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Finding duplicates in a column, prompt user then delete the current one

    OK, how about this?

    Option Explicit
    Sub d()
        Dim Mbox As String
        Dim LR As Long, i As Long, found As Range
        Application.ScreenUpdating = False
         On Error Resume Next
            LR = Cells(Rows.Count, 1).End(xlUp).Row
        For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
            Set found = Range("a:a").Find(what:=Cells(i, "b"), LookIn:=xlValues, LookAt:=xlWhole)
          If Not found Is Nothing Then
                Mbox = MsgBox("Duplicate Found, do you want to delete duplicate", vbYesNo, "Duplicate")
            If Mbox = vbYes Then
              Range(Cells(i, 1), Cells(i, 9)).ClearContents
            Else
              Mbox = vbNo
            Resume Next
            End If
          End If
        Next i
        
        Set found = Nothing
        Application.ScreenUpdating = True
    End Sub
    Last edited by JapanDave; 01-21-2012 at 07:37 AM.
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  14. #14
    Valued Forum Contributor
    Join Date
    02-20-2007
    Location
    South Africa
    MS-Off Ver
    2007
    Posts
    488

    Re: Finding duplicates in a column, prompt user then delete the current one

    Hi JapanDave,

    I think you are just great for sticking with me on this issue, thanks a lot!

    I have added to your code, as follows to clear the duplicate row from Column A to Column J:

    Sub d()
         Dim Mbox As String
        Dim LR As Long, i As Long, found As Range
        Application.ScreenUpdating = False
         On Error Resume Next
            LR = Cells(Rows.Count, 1).End(xlUp).Row
        For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
            Set found = Range("a:a").Find(what:=Cells(i, "b"), LookIn:=xlValues, LookAt:=xlWhole)
          If Not found Is Nothing Then
                Mbox = MsgBox("Duplicate Found, do you want to delete duplicate", vbYesNo, "Duplicate")
            If Mbox = vbYes Then
              Cells(i, 9).ClearContents
              Cells(i, 8).ClearContents
              Cells(i, 7).ClearContents
              Cells(i, 6).ClearContents
              Cells(i, 5).ClearContents
              Cells(i, 4).ClearContents
              Cells(i, 3).ClearContents
              Cells(i, 2).ClearContents
              Cells(i, 1).ClearContents
            Else
              Mbox = vbNo
            Resume Next
            End If
          End If
        Next i
        
        Set found = Nothing
        Application.ScreenUpdating = True
    
    End Sub

    Would there be shorter code to accomplish the same result?

    One more thing is, can your Code be changed only to fire if Duplicates are not empty.

    As it is at the moment, your code regards an empty Row, if there are two or more, as a duplicate, which in Computer terms may be correct. But the user won't understand it as such! they could just ask; "What is two or more of Nothing?"

  15. #15
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Finding duplicates in a column, prompt user then delete the current one

    Hey Winon,

    Try this and see if it is what you are after?

    Option Explicit
    Sub d()
        Dim Mbox As String
        Dim LR As Long, i As Long, found As Range
        Application.ScreenUpdating = False
         On Error Resume Next
            LR = Cells(Rows.Count, 1).End(xlUp).Row
        For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
            Set found = Range("a:a").Find(what:=Cells(i, "b"), LookIn:=xlValues, LookAt:=xlWhole)
              If found <> vbNullString Then
                If Not found Is Nothing Then
                      Mbox = MsgBox("Duplicate Found, do you want to delete duplicate", vbYesNo, "Duplicate")
                  If Mbox = vbYes Then
                    Range(Cells(i, 1), Cells(i, 9)).ClearContents
                  Else
                    Mbox = vbNo
                  Resume Next
                  End If
                End If
              End If
        Next i
        
        Set found = Nothing
        Application.ScreenUpdating = True
    End Sub
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

+ 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.2.0