duplicate entry restriction VBA Code causing issue in other portion

iffi

Board Regular
Joined
Jun 5, 2011
Messages
59
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone
the following vba code is working fine to restrict the user from entering duplicate entry in column B but i dont have any idea why it is giving pop up window error as soon as i copy or delete data in other portion of excel as shown in the attached image

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If WorksheetFunction.CountIf(Range("B:B"), Target) > 1 Then

    MsgBox "Duplicate Entry Is Not Allowed", vbCritical, "Duplicate Entry"
    Target.Value = ""
    Target.Select
    
End If



End Sub

Please rectify the code so it does not cause any problem while working on any action performed in other portion of excel.

Thanks
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    51.3 KB · Views: 3

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Please try this
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

  If Not Intersect(Target, Range("B:B")) Is Nothing Then
    If WorksheetFunction.CountIf(Range("B:B"), Target) > 1 Then
  
      MsgBox "Duplicate Entry Is Not Allowed", vbCritical, "Duplicate Entry"
      Target.Value = ""
      Target.Select
      
    End If
  End If


End Sub
 
Upvote 0
Thanks Jeffrey Mahoney, it is working fine but there is another issue that if i copy two cell together from column a and column b and paste down including the duplicate entry then again the pop up window error shows up as shown in the attached image below. please have a look at this.

Please rectify the code to resolve this issue as well..

Thanks
 

Attachments

  • UntitledD.jpg
    UntitledD.jpg
    23.5 KB · Views: 3
Last edited:
Upvote 0
that if i copy two cell together from column a and column b and paste down including the duplicate entry
in this case will not show message based on the code!
 
Upvote 0
Give this a try:
Note: If you copy/change multiple cells in column B it will only test the first one. If you want to test them all it will take more code. Another option would be to give an error message if you copy/change multiple cells.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
  Dim rngB As Range
  Set rngB = Intersect(Target, Range("B:B"))
  If Not rngB Is Nothing Then
    If WorksheetFunction.CountIf(Range("B:B"), rngB.Cells(1)) > 1 Then
  
      MsgBox "Duplicate Entry Is Not Allowed", vbCritical, "Duplicate Entry"
      Application.EnableEvents = False
      Target.Value = ""
      Target.Select
      Application.EnableEvents = True
    End If
  End If

End Sub
 
Upvote 0
Give this a try:
Note: If you copy/change multiple cells in column B it will only test the first one. If you want to test them all it will take more code. Another option would be to give an error message if you copy/change multiple cells.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
  Dim rngB As Range
  Set rngB = Intersect(Target, Range("B:B"))
  If Not rngB Is Nothing Then
    If WorksheetFunction.CountIf(Range("B:B"), rngB.Cells(1)) > 1 Then
 
      MsgBox "Duplicate Entry Is Not Allowed", vbCritical, "Duplicate Entry"
      Application.EnableEvents = False
      Target.Value = ""
      Target.Select
      Application.EnableEvents = True
    End If
  End If

End Sub
Thank You So Much Alex Blakenburg Sir, it is working perfectly fine now. i have tested it in multiple ways by copying multiple cells along with restricted column B, no more error pop up msg. Thanks once again
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,153
Members
452,891
Latest member
JUSTOUTOFMYREACH

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