Re: Highlight date clashes in Annual leave between staff
The problem is the codes does not like to find only one value in an Array.
I have not found a complete solution, but by not allowing the code to run until there are at least two matching dates , the code (as below) will run.
I hope that works for you at the moment.
Code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Dn As Range, nR As Range, c As Long, p As Long
Dim Ray As Variant, Rng As Range, Q As Variant
Dim Dic As Object, nDic As Object
Dim n As Long 'Date######
Dim R As Range, Dt As Date
Dim nCol As Integer
Dim rCols As Variant, col As Variant, K As Variant
If Not Intersect(Target, Range("A:B,K:L,U:S")) Is Nothing Then
rCols = Array("A", "K", "U")
Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = 1
Set nDic = CreateObject("Scripting.Dictionary")
nDic.CompareMode = 1
nCol = 2
For Each col In rCols
Set Rng = Range(Range(col & "1"), Range(col & Rows.Count).End(xlUp))
Rng.Interior.ColorIndex = xlNone
For Each Dn In Rng.Areas
For Each R In Dn
If IsDate(R) Then
Set nR = IIf(R.Offset(, 1) = "", R, R.Offset(, 1))
For Dt = R To nR
If Not Dic.Exists(Dt) Then
Dic.Add Dt, R
Else
Set Dic(Dt) = Union(Dic(Dt), R)
End If
Next Dt
End If
Next R
Next Dn
Next col
For Each K In Dic.keys
If Dic.Item(K).Count > 1 Then
nDic(Dic(K).Address) = Empty
End If
Next K
If nDic.Count > 1 Then
Ray = Application.Transpose(nDic.keys)
ReDim nray(1 To UBound(Ray, 1) + 1, 1 To 2) As Range
c = 0
For n = 1 To UBound(Ray, 1)
If Not Ray(n, 1) = "" Then
c = c + 1
For p = n To UBound(Ray, 1)
If Not Ray(p, 1) = "" Then
If nray(c, 1) Is Nothing Then
Set nray(c, 1) = Range(Ray(p, 1))
Ray(n, 1) = ""
ElseIf Not Intersect(nray(c, 1), Range(Ray(p, 1))) Is Nothing Then
Set nray(c, 1) = Union(nray(c, 1), Range(Ray(p, 1)))
Ray(p, 1) = ""
End If
End If
Next p
End If
Next n
For n = 1 To c
nCol = nCol + 1
nCol = IIf(nCol = 13, 3, nCol)
nray(n, 1).Interior.ColorIndex = nCol
Next n
End If
End If
End Sub
Display More