Notify user of duplicate rows before deleting

keith05281967

Board Regular
Joined
May 6, 2011
Messages
68
Greetings VBA Guru's,

I recorded a macro to select column A, find and delete any duplicates found. Here is the code:

Columns("A:A").Select
ActiveSheet.Range("$A$1:$A$37").RemoveDuplicates Columns:=1, Header:=xlYes

I need the code to tell the user if duplicate rows were found or not. If found, give the user the option to proceed with deleting or not. I'm sure this is accomplished w/ an If stmt, but I don't know how to word it.

The above code was just a first pass. The most important thing is that it "notify" the user of duplicates before taking any action.


thanks so much. I'm using excel 07.
Also can you please use code examples with explanations?

thanks in advance for your help,
Keith
<!-- / message -->
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi Keith
Try
Code:
Sub remdupes()
Dim ans As String
ans = MsgBox("You are about to remove duplicate values, Do you wish to proceed ??", vbYesNo)
    If ans = 7 Then
        Exit Sub
    End If
ActiveSheet.Range("$A$1:$A$37").RemoveDuplicates Columns:=1, Header:=xlYes
End Sub
 
Upvote 0
Hi, and welcome to the Board.

If you want this to work "whenever anything" is typed in Column A of the sheet.

Maybe this placed in the Worksheet Code Window. Right Click the Sheet tab > View Code and place there.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range
    Dim Ans As Integer
    If Target.Count <> 1 Then Exit Sub
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    If Application.CountIf(Rng, Target) > 1 Then
        Ans = MsgBox("The Value you entered  " & "(" & Target & ")" & "  is a Duplicate in Column A" _
                     & vbCr & "Do you want to alow it?", vbYesNo)
        If Ans = vbNo Then
            ActiveCell.Offset(-1).Clear
        Else
            Exit Sub
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top