Change Cell Color inside Formula

NamssoB

Board Regular
Joined
Jul 8, 2005
Messages
76
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I need to set a cells background color (and potentially the text color), but without using Conditional Formatting.

Column G contains a word (Pending, Complete, Scheduled)
Column H contains a dollar value
Column I is what I am manipulating.

The current formula in Column I is as follows:
Code:
=IF(G30="Pending",H30,IF(G30="Complete",H30,""))

This is super simple - based on what is in G, it copies the dollar value from H to I. What I need to add is that if G30="Pending", I also want to COLOR cell I to be a light yellow background.

1) Is there already a way to do this?
2) If NO, and I use a VBA Function, how do I structure the VBA function so that it works for all cells in Column I, without hard coding a specific cell?

Column I Formula:
Code:
=IF(G30="Pending",SetColor(H30,"Yellow"),IF(G30="Complete",H30,""))

Function Code:
Code:
Function SetColor(strColor as String, dblValue as Double)
   Cell.Color.Background=strColor
   Cell.Value = dblValue
End Function

I figured that I have to also populate the value into the cell because you can't perform two things inside the True leg of the IF statement. Or am I missing something there also?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
As far as I know you can not use a UDF to set formatting like cell color. Why can you not use conditional formatting? you could use a change event something like

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("I30:I37")) Is Nothing Then
    If UCase(Target.Offset(0, -2)) = "PENDING" Then
        Target.Interior.ColorIndex = 6
    End If
End If
End Sub
 
Upvote 0
I have used this API trick to change the cell color from within the UDF .

Put this code in a Standard Module :

Code:
Option Explicit

[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  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
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    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
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  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

You would then use the worksheet formula in Column I as follows :
Code:
=(IF(G30="Pending",SetColor(H30,"Yellow"),IF(G30="Complete",SetColor(H30;"None"),SetColor("","None"))))

The UDF is flexible as to the colors you can use other than yellow.

You could for example set the following colors :

Red for "Pending"
Yellow for "Scheduled"
Green for "Complete"

with this formula :
Code:
=(IF(G30="Pending",SetColor(H30;"Red"),IF(G30="Complete",SetColor(H30;"Green"),SetColor("","Yellow"))))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,227
Members
449,303
Latest member
grantrob

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top