Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA Function to Return True or False based on Cell Background Colors in a Range

I keep a spreadsheet of my timeline at work and when I meet and expect to meet certain milestones. The data (dates) is stored left-to-right and each project has it's own row. The milestones are permantely set and occupy Range(O:AA). My data is color coded as Green (Complete), Orange(Deadline), Blue(Not working on), Red(Not applicable).

What I would like to do is write a function that would check if a cell contained an orange color background(Color index 6) and return a TRUE or FALSE based on that. Basically I want to aggregate all of the deadlines across all of the columns. Eventually I would like to integrate a date check as well so I can see which deadlines are approaching.

Function ScanForColor(Dates As Range) as Boolean
    If ScanForColor.Interior.ColorIndex = 6 Then
        ScanForColor = True
    Else
        ScanForColor = False
End Function

I would like to call the function in a cell like =ScanForColor(O3:AA3) and I will have the ScanForColor function in column AB to hold the values for filtering the document.

like image 676
Ryan B Avatar asked Jul 28 '10 15:07

Ryan B


1 Answers

Something like this will do the trick!

Function ScanForColor(Cells As Range, ColorValue As Integer) As Boolean
    Dim cell As Range
    For Each cell In Cells
        If cell.Interior.ColorIndex = ColorValue Then
            ScanForColor = True
            Exit For
        End If
    Next
End Function

This will allow you to call and test different color values....

like image 81
Graeme Avatar answered Sep 22 '22 10:09

Graeme