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
Last edited by Lifeseeker; 01-23-2012 at 07:24 PM.
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
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
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
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?
Clear cell contents if the next value you enter in col a has a duplicate.
Only checking column a please.
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
oh I see..
hmm maybe deleting the entire row is a better option. I will check it tomorrow.
Thank you
@ 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] .
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
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
@ 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] .
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
@ DaveJapan,
Perhaps the notes I have made in your WB attached, will clear up the misunderstanding.
Thank you for your patience!
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
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?"
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks