Highlight / warn when a value is entered again in the same row?

rjn008

New Member
Joined
May 12, 2023
Messages
27
Office Version
  1. 365
Platform
  1. Windows
Hello there, I am a newbie. I am a good excel user but not a good VBA or any language writer/programmer.

I want to create a VB script so that the below when entered into the cell on the same row, if it already exists to highlight this and a pop up warning if possible, to either accept and continue so it highlights or to have the option of not accepting the entry? Is this possible?

1683873485325.png


So on row 5 where Orange has been entered for a 2nd time, this is when I would like the VB to kick in, it could be on any row and any column? I use a data validation list view on each cell from the cells B20:B30 - can obviously vary, smaller or longer.

This is the mini sheet for you to help with;
Book1.xlsx
ABCD
1Test 21/04/202325/04/202329/04/2023
2
37771
4NameWeek 1Week 2Week 3
5AWhiteOrangeOrange
6BOrangeBlue
7CYellowRed
8DBlueBlack
9ERedBlack
10FPurplePink
11GBlackYellow
12
13
14
15
16
17
18
19
20White
21Orange
22Yellow
23Blue
24Red
25Purple
26Black
27Pink
28Green
29Violet
30Plum
Sheet1
Cell Formulas
RangeFormula
A3A3=COUNTA(A5:A21)
B3:D3B3=COUNTA(B5:B11)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:H11Expression=ROW(A1)=CurrentRowtextNO
Cells with Data Validation
CellAllowCriteria
B5:D11List=$B$20:$B$30
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
@rjn008 Welcome to MrExcel.

Maybe give this a try. Copy /paste code into the sheet's code module within the VB editor.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim Rng As Range

'Dont apply code if it is a change to more than just a single cell
If Target.Cells.Count > 1 Then Exit Sub

'determine changed cell ,'Target', is within sensible range of interest
If Not Intersect(Target, Range("B3:AZ15")) Is Nothing Then  '<< ????? Edit range to suit


    Set Rng = Range(Target.EntireRow.Address)   'Rng set to Target row
    
    If Application.WorksheetFunction.CountIf(Rng, Target) > 1 Then   ' check for duplicate entry in row
           
                Rng.Interior.ColorIndex = 3  'if duplicate, hidhlight row red
         'display message andawait response
       Resp = MsgBox("Do you wish to keep this duplicate enty?", vbYesNo, "DUPLICATE ENTRY!!!")
                
               
               If Not Resp = vbYes Then   'If response is No then
                Application.EnableEvents = False  'Disable event handling otherwise this event will  call itself again
                
                    Target.ClearContents   'clear the Target entry
                     Target.Select  're-select the Target cell forpossible re-entry
                     
                  Application.EnableEvents = True  're-establish event handling
                End If
      'In any event, clear the red highlight
      Rng.Interior.ColorIndex = xlColorIndexNone
      
      
    End If
End If
End Sub
Hope that helps.
 
Upvote 0
@rjn008 Welcome to MrExcel.

Maybe give this a try. Copy /paste code into the sheet's code module within the VB editor.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim Rng As Range

'Dont apply code if it is a change to more than just a single cell
If Target.Cells.Count > 1 Then Exit Sub

'determine changed cell ,'Target', is within sensible range of interest
If Not Intersect(Target, Range("B3:AZ15")) Is Nothing Then  '<< ????? Edit range to suit


    Set Rng = Range(Target.EntireRow.Address)   'Rng set to Target row
   
    If Application.WorksheetFunction.CountIf(Rng, Target) > 1 Then   ' check for duplicate entry in row
          
                Rng.Interior.ColorIndex = 3  'if duplicate, hidhlight row red
         'display message andawait response
       Resp = MsgBox("Do you wish to keep this duplicate enty?", vbYesNo, "DUPLICATE ENTRY!!!")
               
              
               If Not Resp = vbYes Then   'If response is No then
                Application.EnableEvents = False  'Disable event handling otherwise this event will  call itself again
               
                    Target.ClearContents   'clear the Target entry
                     Target.Select  're-select the Target cell forpossible re-entry
                    
                  Application.EnableEvents = True  're-establish event handling
                End If
      'In any event, clear the red highlight
      Rng.Interior.ColorIndex = xlColorIndexNone
     
     
    End If
End If
End Sub
Hope that helps.
Thank you, on my dummy data that works, on my actual data I have this bit of code in. So do I just put the code above in the same VB box or if not please advise?

1683883434379.png
 
Upvote 0
Yes, I would say so. They relate to different worksheet events.

Test it on a backed-up copy.
 
Upvote 0
Yes, I would say so. They relate to different worksheet events.

Test it on a backed-up copy.
@Snakehips yes I have it working many thanks! The only thing is your highlight rule doesn't work as I have my current row highlight rule in place. But the warning box appears which is as useful. (y)
 
Upvote 0
Morning @Snakehips - On the rule are you able to set up so that if you accept the entry it just highlights the cell rather than the row?

1684131081473.png

Presume the 'EntireRow' needs change but does just putting 'Cell' in there work?
 
Upvote 0
And keep the cell highlighted is what I mean even if you select another cell, so it stands out on the sheet?
 
Upvote 0
Morning ;)
The code still needs to set 'Rng'as the entire row so that the COUNTIF function will work as number of entries in the row expands.
'Target' is the variable that is auto-designated for the triggering range of event handling code so you now need the highlighting part of your code to apply to Target.

Try the revision below.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim Rng As Range

'Dont apply code if it is a change to more than just a single cell
If Target.Cells.Count > 1 Then Exit Sub

'determine changed cell ,'Target', is within sensible range of interest
If Not Intersect(Target, Range("B3:AZ15")) Is Nothing Then  '<< ????? Edit range to suit


    Set Rng = Range(Target.EntireRow.Address)   'Rng set to Target row
    
    If Application.WorksheetFunction.CountIf(Rng, Target) > 1 Then   ' check for duplicate entry in row
           
                Target.Interior.ColorIndex = 3  'if duplicate, hidhlight CELL red
         'display message andawait response
       Resp = MsgBox("Do you wish to keep this duplicate enty?", vbYesNo, "DUPLICATE ENTRY!!!")
                
               
               If Not Resp = vbYes Then   'If response is No then
                Application.EnableEvents = False  'Disable event handling otherwise this event will  call itself again
                
                    Target.ClearContents   'clear the Target entry
                    Target.Interior.ColorIndex = xlColorIndexNone 'clear the highlight
                     Target.Select  're-select the Target cell forpossible re-entry
                     
                  Application.EnableEvents = True  're-establish event handling
                End If

      'If response is Yes then do nothing and the duplicate cell will remain highlighted
            
    End If
End If
End Sub
 
Upvote 0
Morning @Snakehips - if I wanted to create the same warnings but down a column rather than across a row, what part of the code would need changing?

I presume this but change to something Column named?
1684393737992.png
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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