Hi and welcome to ExcelForum,
Try the following:
Option Explicit
Sub ColorCellBasedOnPresentTime()
Const nColorIndexRED = 3
Const nColorIndexGREEN = 4
Const nColorIndexYELLOW = 6
Dim myTime As Date
Dim iColorIndex As Long
Dim iHour As Long
Dim bNeedMessage As Boolean
'Get the Present Time
'Get the Present Hour
myTime = Now()
iHour = Hour(myTime)
'Initialize the 'Need Message' Flag
bNeedMessage = False
'Determine which Color is needed
'Determine if a Message should be displayed.
If iHour > 17 Then
bNeedMessage = True
iColorIndex = nColorIndexRED
ElseIf iHour > 14 Then
iColorIndex = nColorIndexRED
ElseIf iHour > 11 Then
iColorIndex = nColorIndexYELLOW
Else
iColorIndex = nColorIndexGREEN
End If
'Color the Cell
ThisWorkbook.Sheets("Measurements").Unprotect Password:=""
Range("H10").Interior.ColorIndex = iColorIndex
ThisWorkbook.Sheets("Measurements").Protect Password:=""
'Display the message if needed
If bNeedMessage = True Then
MsgBox "Must Do Now!"
End If
End Sub
To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx
Debugger Secrets:
a. Press 'F8' to single step (goes into subroutines and functions).
b. Press SHIFT 'F8' to single step OVER subroutines and functions.
c. Press CTRL 'F8' to stop at the line where the cursor is.
d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
output to the IMMEDIATE WINDOW.
f. Select View > Locals to see all variables while debugging.
g. To automatically set a BREAKPOINT at a certain location put in the line:
'Debug.Assert False'
h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
if i >= 20 and xTV20 > 99.56 then
Debug.Assert False
endif
i. A variable value will be displayed by putting the cursor over the variable name.
Lewis
Bookmarks