Option Explicit
#If VBA7 Then
Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
Declare PtrSafe Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Declare PtrSafe Function GlobalGetAtomName Lib "kernel32" Alias "GlobalGetAtomNameA" (ByVal nAtom As Integer, ByVal lpBuffer As String, ByVal nSize As Long) As Long
#Else
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Declare Function GlobalGetAtomName Lib "kernel32" Alias "GlobalGetAtomNameA" (ByVal nAtom As Integer, ByVal lpBuffer As String, ByVal nSize As Long) As Long
#End If
Public Function SetColor(ByVal Value As Variant, ByVal BackGroundColor As String) As Variant
SetTimer Application.hwnd, GlobalAddAtom(Application.Caller.Address(External:=True) & "*" & BackGroundColor), 0, AddressOf ChangeColor
SetColor = Value
End Function
Sub ChangeColor(ByVal hwnd As Long, ByVal uMsg As Long, ByVal nIDEvent As Long, ByVal dwTimer As Long)
Dim sBuffer As String, lRet As Long, lColorIndex As Long
On Error Resume Next
KillTimer hwnd, nIDEvent
sBuffer = Space(256)
lRet = GlobalGetAtomName(nIDEvent, sBuffer, Len(sBuffer))
sBuffer = Left(sBuffer, lRet)
Select Case UCase(Split(sBuffer, "*")(1))
Case "YELLOW"
lColorIndex = 6
Case "GREEN"
lColorIndex = 14
Case "BLUE"
lColorIndex = 23
Case "RED"
lColorIndex = 3
Case "CYAN"
lColorIndex = 33
Case "MAGENTA"
lColorIndex = 47
Case "NONE"
lColorIndex = xlColorIndexNone
End Select
Range(Split(sBuffer, "*")(0)).Interior.ColorIndex = lColorIndex
End Sub