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

rjn008

New Member
Joined
May 12, 2023
Messages
17
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
 
Morning @rjn008 In essence, yes.
However, this is code for the sheet change event and there can only be one set of code per event type.
So you will need that code to deal with both options, row or column. There are various ways you can do this and it may depend upon your need to separate out a row duplicate from a column duplicate? The below is not tested but should catch either row and/or column duplicates and highlight either but without saying which it is. It would be simple enough to have it message 'Column Dup' or 'Row Dup' if it is necessary.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim RowRng As Range
Dim ColRng 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 RowRng = Range(Target.EntireRow.Address)   ' set to Target row
    Set ColRng = Range(Target.EntireColumn.Address)   ' set to Target Column
    
    ' check for duplicate entry in row or column
   Debug.Print Application.WorksheetFunction.CountIf(RowRng, Target) > 1 Or Application.WorksheetFunction.CountIf(ColRng, Target) > 1
    
    If Application.WorksheetFunction.CountIf(RowRng, Target) > 1 Or Application.WorksheetFunction.CountIf(ColRng, Target) > 1 Then
           
                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

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi again @Snakehips, I'll give that a go however, my apologies for not being specific, it is just another sheet I am using where by it would just be a list in a column rather than a row, so can you do a script just for a column event?
 
Upvote 0
Oh, and again I would like to highlight the cell not the column upon error?
 
Upvote 0
If it's for a separate sheet, dedicated to find row duplicates then do as you suggested.
Copy your column based code, paste it into new sheet code pane.
Edit the Set Rig = line to be .....
VBA Code:
Set Rng = Range(Target.EntireRow.Address)   'Rng set to Target row
 
Upvote 0
Solution

Forum statistics

Threads
1,215,087
Messages
6,123,046
Members
449,092
Latest member
ikke

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