Find if a number already exists in column

dinkss

Board Regular
Joined
Aug 25, 2020
Messages
129
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi All,

I'm trying to get formula or macro to check if number already exists in a column G:G.
If an employee will enter the same number I would like to have a pop up message (Alert) that number entered already exists.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi All,

I'm trying to get formula or macro to check if number already exists in a column G:G.
If an employee will enter the same number I would like to have a pop up message (Alert) that number entered already exists.
I'm using xlookup to enter the number in G column but it doesn't show duplicated values :(
 
Upvote 0
How about using Conditional Formatting to highligh duplicated values?
If you select Column G, and use the following Conditional Formatting formula:
Excel Formula:
=COUNTIF(G:G,G1)>1
and choose your desired formatting color, it will highlight any duplicated values.
 
Upvote 0
How about using Conditional Formatting to highligh duplicated values?
If you select Column G, and use the following Conditional Formatting formula:
Excel Formula:
=COUNTIF(G:G,G1)>1
and choose your desired formatting color, it will highlight any duplicated values.
That works great but how can I get pop up message when employee will try to enter value that already exists in columns G?
Basically how can I get excel to show an error message when using xlookup (employee enters text in F column, and xlookup enters number in G column - so I need an error message if the same number will appear in G column).

Is this possible?
 
Upvote 0
OK, I think you probably need VBA to do that.
Right-click on the tab sheet name at the bottom of the screen, select "View Code", and paste this code in the VB Editor window that pops up:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    Dim r As Long
   
'   Set entry range to look at
    Set rng = Intersect(Target, Range("F:F"))
   
'   Exit if no cells updated in watched range
    If rng Is Nothing Then Exit Sub
   
'   Check updated cell for duplicates
    For Each cell In rng
'       Get row of updated cell
        r = cell.Row
'       Check to see if value populated in column G already exists in column G
        If Application.WorksheetFunction.CountIf(Range("G:G"), Range("G" & r)) > 1 Then
            MsgBox "Entry in column F of row " & r & " causes a duplicate in column G", vbOKOnly, "ENTRY ERROR!"
'           ***UNCOMMENT THE FOLLOWING 3 LINES IF YOU WANT TO CLEAR COLUMN F VALUE***
            'Application.EnableEvents = False
            'cell.Clear
            'Application.EnableEvents = True
        End If
    Next cell
   
End Sub
Now, if any manual entry you make in column F causes a duplicate to occur in column G, it will give you an error message.

If you want it to also automatically remove the value from column F that you just entered that caused the duplicate value in column G, uncomment these three lines in the code by removing the single-quote mark that is at the front of each line:
VBA Code:
            'Application.EnableEvents = False
            'cell.Clear
            'Application.EnableEvents = True
 
Upvote 0
Solution
OK, I think you probably need VBA to do that.
Right-click on the tab sheet name at the bottom of the screen, select "View Code", and paste this code in the VB Editor window that pops up:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    Dim r As Long
  
'   Set entry range to look at
    Set rng = Intersect(Target, Range("F:F"))
  
'   Exit if no cells updated in watched range
    If rng Is Nothing Then Exit Sub
  
'   Check updated cell for duplicates
    For Each cell In rng
'       Get row of updated cell
        r = cell.Row
'       Check to see if value populated in column G already exists in column G
        If Application.WorksheetFunction.CountIf(Range("G:G"), Range("G" & r)) > 1 Then
            MsgBox "Entry in column F of row " & r & " causes a duplicate in column G", vbOKOnly, "ENTRY ERROR!"
'           ***UNCOMMENT THE FOLLOWING 3 LINES IF YOU WANT TO CLEAR COLUMN F VALUE***
            'Application.EnableEvents = False
            'cell.Clear
            'Application.EnableEvents = True
        End If
    Next cell
  
End Sub
Now, if any manual entry you make in column F causes a duplicate to occur in column G, it will give you an error message.

If you want it to also automatically remove the value from column F that you just entered that caused the duplicate value in column G, uncomment these three lines in the code by removing the single-quote mark that is at the front of each line:
VBA Code:
            'Application.EnableEvents = False
            'cell.Clear
            'Application.EnableEvents = True
This works fantastic! You are amazing! Thank you very much!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0
You are welcome.
Glad I was able to help!
Hi Joe4 again,

Just wondering would it be possible to add to above script option to highlight cell in yellow if duplicate is found?

Can something like this be added:
cell.Interior.Color = vbYellow
 
Upvote 0
Hi Joe4 again,

Just wondering would it be possible to add to above script option to highlight cell in yellow if duplicate is found?

Can something like this be added:
cell.Interior.Color = vbYellow
It is working :) I just checked :) Unless there's other way of doing it?
 
Upvote 0
It is working :) I just checked :) Unless there's other way of doing it?
You could just leave the code "as-is" and just use the Conditional Formatting I showed you in my initial posts to highlight the duplicates yellow.
The advantage to using Conditional Formatting is if you fix the issue, the color will go away automatically. Otherwise, you would need to add more code to your VBA code to "undo" the highlighting if the duplicate is fixed.
 
Upvote 0

Forum statistics

Threads
1,216,091
Messages
6,128,775
Members
449,468
Latest member
AGreen17

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