June 4, 2016

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

2 thoughts on “How to avoid duplicate entries in Excel using Countif in VBA

  1. Sir, i want to find how many duplicate values are available in my excel sheet in msgbox by clicking on command button .

    Please give code for it

Comments are closed.