This forum has been moved to TeachExcel.com
Ask all future questions in the New Excel Forum.
ExcelKey
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRow As Long
Dim rng As Range
'limit cell monitoring
If Target.Row < 7 Or Target.Count <> 1 Then Exit Sub
'columns to check
If Target.Column <> 1 And Target.Column <> 11 And Target.Column <> 12 And Target.Column <> 13 Then Exit Sub
'establish last row to check for duplicate
LastRow = Cells(Rows.Count, Target.Column).End(xlUp).Row
'prevent this event from calling itself
Application.EnableEvents = False
'checking col A '<~~~~~~ column 1
If Target.Column = 1 Then
'rng of concern
Set rng = Range("A7:A" & LastRow)
'if target is not empty
If Target.Value <> "" Then
'count how many times target appears in rng
If Application.WorksheetFunction.CountIf(rng, Target.Value) = 1 Then
'it's targets first occurrance, put date/time into adjacent cell
Target.Offset(0, 1).Value = Now + 8 / 24
'remove any existing cell fill color
Target.Interior.ColorIndex = 0
Else
'it's a duplicate
'put date/time in adjacent cell
Target.Offset(0, 1).Value = Now + 8 / 24
'indicate it's a duplicate
Range("A3").Value = Target.Value
'color the just entered duplicates cell
Target.Interior.ColorIndex = 44
End If
'if target is empty ie:been deleted
Else
'remove date/time
Target.Offset(0, 1).Value = ""
'clear duplicate indication
Range("A3").Value = ""
'remove cell color
Target.Interior.ColorIndex = 0
End If
'checking col K '<~~~~~~ column 11
ElseIf Target.Column = 11 Then
'rng of concern
Set rng = Range("K7:K" & LastRow)
'do what you want for column K
'count how many times target appears in rng
If Application.WorksheetFunction.CountIf(rng, Target.Value) = 1 Then
'it's first not duplicate
'clear anything in K3
Range("K3").Value = ""
'reset target color
Target.Interior.Color = RGB(217, 217, 217)
Else
'indicate it's a duplicate
Range("K3").Value = Target.Value
'color the just entered duplicate cell
Target.Interior.ColorIndex = 44
End If
'checking col L "<~~~~~~ column 12
ElseIf Target.Column = 12 Then
'rng of concern
Set rng = Range("L7:L" & LastRow)
'count how many times target appears in rng
If Application.WorksheetFunction.CountIf(rng, Target.Value) = 1 Then
'it's first not duplicate
'clear anything in L3
Range("N3").Value = ""
'reset target color
Target.Interior.Color = RGB(217, 217, 217)
Else
'indicate it's a duplicate
Range("L3").Value = Target.Value
'color the just entered duplicate cell
Target.Interior.ColorIndex = 44
'checking col M "<~~~~~~ column 13
ElseIf Target.Column = 13 Then
'rng of concern
Set rng = Range("M7:M" & LastRow)
'count how many times target appears in rng
If Application.WorksheetFunction.CountIf(rng, Target.Value) = 1 Then
'it's first not duplicate
'clear anything in M3
Range("M3").Value = ""
'reset target color
Target.Interior.Color = RGB(217, 217, 217)
Else
'indicate it's a duplicate
Range("M3").Value = Target.Value
'color the just entered duplicate cell
Target.Interior.ColorIndex = 44
End If
End If
'make sure events are re-enabled
Application.EnableEvents = True
End Sub
Return to Macros and VBA Questions
Users browsing this forum: Google [Bot] and 117 guests