Use my existing code but alter part of it

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
I am using the code below to inform me of any duplicates in column AB of which it works well.
I run the code & if any duplicates are found i see the Msgbox saying DUPLICATE KEY CODE FOUND IN CELL AB27
Then i click OK & the message box will close.

The alteration i need please is for me to still be advised of the cell location BUT when i click OK the code then needs to continue down the column as opposed to exiting & once done a Msgbox with something like search complete etc

Example.
DUPLICATE KEY CODE FOUND IN CELL AB27
I click OK then the code carries on down the page.

DUPLICATE KEY CODE FOUND IN CELL AB54
I click OK then the code carries on down the page.

DUPLICATE KEY CODE FOUND IN CELL AB99
I click OK then the code carries on down the page.



Rich (BB code):
Private Sub DuplicateKeyCode_Click()
    Dim cell As Range

    With Intersect(ActiveSheet.Columns("AB"), ActiveSheet.UsedRange)
        For Each cell In .Cells
            If WorksheetFunction.CountIf(.Resize(cell.Row - .Rows(1).Row + 1), cell.Value) > 1 Then
             MsgBox "DUPLICATE KEY CODE " & cell.Value & "  FOUND IN CELL " & cell.Address(False, False), vbCritical, "DUPLICATE ITEM NUMBER CHECKER"
             cell.Select
             Exit Sub
           End If
            
        Next cell

    End With
    MsgBox "NO DUPLICATES WERE FOUND", vbInformation, "DUPLICATE KEY CODE CHECKER"

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
try this:
VBA Code:
Private Sub DuplicateKeyCode_Click()
    Dim cell As Range
   dupfound = False
  With Intersect(ActiveSheet.Columns("AB"), ActiveSheet.UsedRange)
        For Each cell In .Cells
            If WorksheetFunction.CountIf(.Resize(cell.Row - .Rows(1).Row + 1), cell.Value) > 1 Then
             cell.Select
             answer = MsgBox("DUPLICATE KEY CODE " & cell.Value & "  FOUND IN CELL " & cell.Address & vbNewLine & "Do you want to continue?", vbYesNo + vbQuestion, "DUPLICATE ITEM NUMBER CHECKER")
             dupfound = True
             If answer = vbNo Then
                  Exit Sub
             End If
           End If
          
        Next cell

    End With
    If Not (dupfound) Then
    MsgBox "NO DUPLICATES WERE FOUND", vbInformation, "DUPLICATE KEY CODE CHECKER"
    Else
   Msgbox "Complete"
    End If
End Sub
 
Upvote 0
Solution
Hi,
Thanks for that.

It works BUT i see $ signs around the cell reference. $AB$15 not AB15 ??
See attached photo.
Thanks

4028.jpg
 
Upvote 0
Hi,
Thanks for that.

It works BUT i see $ signs around the cell reference. $AB$15 not AB15 ??
See attached photo.
Thanks

View attachment 68821
If you want to get rid of the dollar signs, just replace the "cell.Address" of this line to match your format you used in your original code, i.e.
change this:
Rich (BB code):
answer = MsgBox("DUPLICATE KEY CODE " & cell.Value & "  FOUND IN CELL " & cell.Address & vbNewLine & "Do you want to continue?", vbYesNo + vbQuestion, "DUPLICATE ITEM NUMBER CHECKER")
to this:
Rich (BB code):
answer = MsgBox("DUPLICATE KEY CODE " & cell.Value & "  FOUND IN CELL " & cell.Address(False, False) & vbNewLine & "Do you want to continue?", vbYesNo + vbQuestion, "DUPLICATE ITEM NUMBER CHECKER")
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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