warning for duplicate value in a specific column

Bijan2048

New Member
Joined
Apr 16, 2023
Messages
9
Office Version
  1. 2007
Platform
  1. Windows
Hi experts
I am looking for a VB script to control cells of a specific column( B ) that user Don't enter a duplicate value

A B C
MC 123
DF 456
DF 789
LG 123 ====> WARNING!! Duplicate value

and Don't let the user to continue unless change it to unique value
Thanks in advance
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
try this in the sheet where you want to enforce the no-duplicate rule for column B.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim cell As Range
    Dim value As String
    
    Set rng = Range("B:B") ' Specify the range of the column to check for duplicates
    
    If Not Intersect(Target, rng) Is Nothing Then ' Check if the changed cell is in the specified range
        For Each cell In Intersect(Target, rng)
            value = cell.value
            If WorksheetFunction.CountIf(rng, value) > 1 Then
                MsgBox "Duplicate value detected in column B: " & value
                Application.EnableEvents = False ' Disable events to prevent infinite loop
                cell.ClearContents ' Remove the duplicate value
                Application.EnableEvents = True ' Re-enable events
            End If
        Next cell
    End If
End Sub
 
Upvote 0
You could do this using data validation in column B. Simply highlight column B. Click on Data Validation .... Custom and enter this formula in the formula box: =(COUNTIF($B$1:$B1, $B1)<2)
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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