How to avoid duplicate entries in Excel using Countif in VBA

How to avoid duplicate entries in an Excel worksheet using the COUNTIF function in VBA. Here we have two options:
1. We warn the user of the duplicate entry and remove the entry automatically
2. We only inform the user of the duplicate entry. It is up to him to take remedial action if required

Watch the training video:

You can also learn about the countif function in VBA by watching this video on YouTube.

Here’s the complete VBA code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Application.CountIf(Range(“E:E”), Target) > 1 Then
‘MsgBox “Duplicate data!”, vbCritical, “Remove Data”
MsgBox “Duplicate entry!”, vbInformation, “Change Entry”
‘Target.Value = “”
End If

End sub

Further reading:

Countif function

Avoid Duplicate Entries in Excel

Generate Test Paper from Question Bank Using Excel VBA