Hi there,
Insert the following code into the ThisWorkbook VBA CodeModule and see if it does what you need:
Option Explicit
'=========================================================================================
'=========================================================================================
Const msDATE_CELLS As String = "F8:AC47"
'=========================================================================================
'=========================================================================================
Private Sub Workbook_Open()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
Call SetTabColour(wks:=wks)
Next wks
End Sub
'=========================================================================================
'=========================================================================================
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Call SetTabColour(wks:=Sh)
End Sub
'=========================================================================================
'=========================================================================================
Private Sub SetTabColour(wks As Worksheet)
If mbOverdue(wks:=wks) = True Then
wks.Tab.Color = vbRed
Else: wks.Tab.Color = vbGreen
End If
End Sub
'=========================================================================================
'=========================================================================================
Private Function mbOverdue(wks As Worksheet) As Boolean
Const iOVERDUE As Integer = 182
Dim rDateCells As Range
Dim bOverDue As Boolean
Dim rCell As Range
bOverDue = False
Set rDateCells = wks.Range(msDATE_CELLS)
For Each rCell In rDateCells.Cells
If IsDate(rCell.Value) Then
If (Int(Now() - rCell.Value)) > iOVERDUE Then
bOverDue = True
Exit For
End If
End If
Next rCell
mbOverdue = bOverDue
End Function
The highlighted value can be altered to suit your own requirements.
Opening the workbook will set the tab colour of each worksheet appropriately.
When date values are changed on a worksheet, the tab colour of that worksheet will be set when the worksheet is deactivated, i.e. when a different worksheet is selected.
By including the following code it is possible to have the tab colour update each time the value of a date cell is changed, but it adds an (in my opinion) unnecessary overhead unless this facility is essential.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim rDateCells As Range
Dim iNoOfCells As Integer
Dim wks As Worksheet
On Error Resume Next
iNoOfCells = 0
iNoOfCells = Target.Cells.Count
On Error GoTo 0
If iNoOfCells = 1 Then
If Target.Value <> 0 Then
Set wks = Target.Parent
Set rDateCells = wks.Range(msDATE_CELLS)
If Not Intersect(Target, rDateCells) Is Nothing Then
Call SetTabColour(wks:=wks)
End If
End If
End If
End Sub
Hope this helps - please let me know how you get on.
Regards,
Greg M
Bookmarks