Prevent the same value from repeating in certain Rows

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Welcome

How can I implement the same command and prevent the same value from repeating in multiple rows?

Example

A5:G5 ; A8:G8 ; A11:G11


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim strTargetColumn As String
   Dim nTargetRow As Integer
   Dim nLastRow As Integer
   Dim strMsg As String

   strTargetColumn = Split(Target.Address(, False), "$")(0)
   nTargetRow = Split(Target.Address(, False), "$")(1)
   nLastRow = ActiveSheet.Range(strTargetColumn & ActiveSheet.Rows.Count).End(xlUp).Row

   For nRow = 5 To nLastRow
       If nRow <> nTargetRow Then
          If ActiveSheet.Range(strTargetColumn & nRow).Value = Target.Value Then
             strMsg = "The value has been entered in the same column!"
             MsgBox strMsg, vbExclamation + vbOKOnly, "Duplicate Values"
             Target.Select
             Exit For
          End If
       End If
   Next
End Sub
 

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.
Hi,
see if this update to your code does what you want

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng         As Range, Area As Range
    
    Const strMsg    As String = "The value has already been entered in the same row!"
    
    Set rng = Me.Range("A5:G5,A8:G8,A11:G11")
    
    On Error GoTo myerror
    If Not Intersect(Target, rng) Is Nothing Then
        Application.EnableEvents = False
        For Each Area In rng.Areas
            If Application.CountIf(Area, Target.Value) > 1 Then
                MsgBox Target.Value & Chr(10) & strMsg, 48, "Duplicate Entry"
                Application.Undo
                Exit For
            End If
        Next Area
        
    End If
myerror:
    Application.EnableEvents = True
End Sub

Dave
 
Upvote 0
Solution
Hi,
see if this update to your code does what you want

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng         As Range, Area As Range
   
    Const strMsg    As String = "The value has already been entered in the same row!"
   
    Set rng = Me.Range("A5:G5,A8:G8,A11:G11")
   
    On Error GoTo myerror
    If Not Intersect(Target, rng) Is Nothing Then
        Application.EnableEvents = False
        For Each Area In rng.Areas
            If Application.CountIf(Area, Target.Value) > 1 Then
                MsgBox Target.Value & Chr(10) & strMsg, 48, "Duplicate Entry"
                Application.Undo
                Exit For
            End If
        Next Area
       
    End If
myerror:
    Application.EnableEvents = True
End Sub

Dave
Thank you very much, this is exactly what I needed...
genius.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
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