1

My coding checks the entire rows of E, G, I, K, M, O, Q and S.

I need to remove the 1st 3 Rows from the Equation.

Because I need to maintain heading which having duplicates.

Requirment

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Value = "" Then Exit Sub
    If Intersect(Target, Range("E:E, G:G, I:I , K:K, M:M, O:O, Q:Q, S:S")) Is Nothing Then Exit Sub

    If Application.CountIf(Target.EntireRow, Target.Value) > 1 Then
        Application.EnableEvents = False
        Application.Undo
        MsgBox "Don't duplicate values in a row!"
        Application.EnableEvents = True
    End If
End Sub
Community
  • 1
  • 1
  • **1.** `If Target.Cells.CountLarge > 1 Then Exit Sub` **2.** `If Target.Row < 4 Then Exit Sub` **3.** Also do proper error handling else you have a possibility that the code will not run the next time. You may want to see [THIS](https://stackoverflow.com/questions/13860894/why-ms-excel-crashes-and-closes-during-worksheet-change-sub-procedure/13861640#13861640) – Siddharth Rout Nov 06 '22 at 04:06
  • If Possible Please help to edit full code with your one – indika prasanna Nov 06 '22 at 05:03

1 Answers1

2

Don't you just want this?

If Target.Row <= 3 Then Exit Sub 

EDIT: The full sub (by request):

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Cells.Count > 1 Then Exit Sub ' checks that selection is one cell
  If Target.Row <= 3 Then Exit Sub ' checks that selection row is > 3
  If Target.Value = "" Then Exit Sub
  If Intersect(Target, Range("E:E, G:G, I:I , K:K, M:M, O:O, Q:Q, S:S")) Is Nothing Then Exit Sub
  
  If Application.CountIf(Target.EntireRow, Target.Value) > 1 Then
    Application.EnableEvents = False
    Application.Undo
    MsgBox "Don't duplicate values in a row!"
    Application.EnableEvents = True
  End If
End Sub
dcromley
  • 1,373
  • 1
  • 8
  • 23