How to prevent duplicate Entries in excel through VBA

 VBA codes are follow...

Dim gsh As Worksheet

Set gsh = Sheets("Sheet1")

Dim x As Long

x = gsh.Range("A" & Rows.Count).End(xlUp).Row + 1

If Application.WorksheetFunction.CountIf(gsh.Range("C18", "C" & x), gsh.Range("E7")) > 0 Then

 MsgBox "This Bill Already Exist,Please try another"

 Exit Sub

 End If



To view video support then go to my Youtube Channel and subscribe

Click here to view my channel

If you want to prevent duplicate entries without any VBA Codes

For example, I am entering identify card number into a range of cells (A1:A20), and I want to make sure all item numbers entered in range A1:A20 are unique. To prevent duplicates, follow these steps:

1. Select the range of cells that will contain the item numbers.

2. Go to Data > Data Validation > Data Validation. See screenshot:

doc-prevent-duplicate-entries1

3. And a Data Validation dialog box will display. Click the Settings tab, then click drop down list under Allow, choose Custom, and then enter this formula “=COUNTIF($A$1:$A$20,A1)=1” into the Formula box. See screenshot:

doc-prevent-duplicate-entries2

4. And then click Error Alert tab, under the Title box, enter “Duplicate Entry”, and enter the proper message in the Error message box, such as “The value was already entered. All Item Numbers must be unique. Please try again.”

doc-prevent-duplicate-entries3

5. Click OK to finish it.

Now when you enter a duplicate number, Excel will prevent the entry and alert the user with an error message, it will show up like this:

doc-prevent-duplicate-entries4


No comments:

Post a Comment