Solved

find out if a cell has yellow fill

Posted on 2016-11-01
8
53 Views
Last Modified: 2016-11-03
i need a formula to test if a cell has been filled with yellow.

any ideas?
0
Comment
Question by:finnstone
8 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41869279
In the Immediate window of Visual Basic, type

?Range("A1").interior.color ' Change A1to the address of a yellow cell

That will return a 6 to 8 digit number and you can use that number when looking at other cells.
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 41869979
The tip to the Range().Interior.Color property is basically correct, here's just some clarification:

The returned value is the value returned by the RGB()  function for the respective color, and it consists of the values for Red, Green and Blue in that color. The help for RGB() shows some examples for that, and "classic" Yellow is defined as RGB(255,255,0) or 65535.

Internal it's represented as a 3 Byte integer value, where the right byte (the least significant) represents red, the middle one represents green, and the right one (the most significant) represents blue.

RGB(255,0,0) = 255 = Red
RGB(0,255,0) = 65280 = Green
RGB(0,0,255) = 16711680 = Blue
RGB(0,0,0) = 0 = Black
RGB(255,255,255) = White = 16777215

With that in mind, the minimum length of that number is 1 (for values ranging from 0 (Black) to 8 (an almost invisible faint red).

To check for a yellow cell, the statement would be
if Range(address).Interior.Color = RGB(255,255,0) then (...)

Open in new window

0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41870104
As far as I know it is not possible in standard formulas. You could possibly use a User Defined Function (UDF) which is basically a Visual Basic script that is triggered in the same way as a formula.

Alternatively, looking at it differently, is there a quantifiable reason why a cell would be coloured yellow?
Could that reason for the colour be converted to a Conditional Format so that a cell will automatically go yellow when a condition is met? If that were the case, you could then test for the condition that triggers the format rather than testing for the format itself.

Thanks
Rob H
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:finnstone
ID: 41872045
what can i type in excel to make any of these work?
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 41872095
The most easy way is a user defined function. Open the VBA editor, insert a new module and paste the following code into:

Public Function IsCellYellow(r As Range) As Variant

    If r.Cells.Count > 1 Then
        IsCellYellow = CVErr(xlErrRef)
        Exit Function
    End If
        
    IsCellYellow = r.Interior.Color = RGB(255, 255, 0)

End Function

Open in new window


Use as explained in the attached example workbook.

The function allows a reference to exactly 1 cell and checks if the background is yellow. The result is returned as boolean (true/false). If more than 1 cell is referenced, an Excel error is returned. The function could be used at any place where a boolean value is allowed (i. e. IF(), IIF(), .... )

Hope that helps.
0
 
LVL 14

Accepted Solution

by:
frankhelk earned 500 total points
ID: 41872104
P.S.: To provide more amusement to the crowd, here's the luxury version of the function, which allows to check for any desired color:

Public Function IsCellColor(r As Range, Red As Integer, Green As Integer, Blue As Integer) As Variant

    If r.Cells.Count > 1 Then
        IsCellYellow = CVErr(xlErrRef)
        Exit Function
    End If
    
    If (Red < 0) Or (Red > 255) _
        Or (Green < 0) Or (Green > 255) _
        Or (Blue < 0) Or (Blue > 255) Then
        IsCellYellow = CVErr(xlErrValue)
        Exit Function
    End If
            
    IsCellYellow = r.Interior.Color = RGB(255, 255, 0)

End Function

Open in new window


Should be mostly self-explaining ...  ;-)
0
 

Author Comment

by:finnstone
ID: 41872108
thank you!
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 41872119
Oops - mea maxima culpa ... I've made a quick shot and produced an error ... it takes arguments but still checks for yellow. The function should read

Public Function IsCellColor(r As Range, Red As Integer, Green As Integer, Blue As Integer) As Variant

    If r.Cells.Count > 1 Then
        IsCellYellow = CVErr(xlErrRef)
        Exit Function
    End If
    
    If (Red < 0) Or (Red > 255) _
        Or (Green < 0) Or (Green > 255) _
        Or (Blue < 0) Or (Blue > 255) Then
        IsCellYellow = CVErr(xlErrValue)
        Exit Function
    End If
            
    IsCellYellow = r.Interior.Color = RGB(Red, Green, Blue)

End Function

Open in new window


( Just 1 line changed, the final test ...)

And here's the promised example workbook, too: Example.xlsm
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Split Employee Name into Lname Fname Mname 3 17
Do Wend Macro not working 22 36
Excel 2007 VB Code for GCF 7 16
Countdown Timer 2 17
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question