Test for Color

sachavez

Active Member
Joined
May 22, 2009
Messages
469
Excel 2003

Need code to look at range F10:F21 and determine if any cells in the range are yellow. If "true," need cell F2 to turn yellow and include message saying "True."

Can you help?

Thanks
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try

Code:
Sub test()
Dim c As Range
For Each c In Range("F10:f21")
    If c.Interior.ColorIndex = 6 Then
        With Range("F2")
            .Value = True
            .Interior.ColorIndex = 6
        End With
        Exit For
    End If
Next c
End Sub
 
Upvote 0
Thanks, Peter.

Could you update for a worksheet change? I'd like to put the code in the Sheet1 tab.

Steve
 
Upvote 0
That won't work if the color is from Conditional Formatting and changing a fill color won't trigger a worksheet change event.
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F10:f21")) Is Nothing Then
    If Target.Value = "color me yellow" Then
        With Range("F2")
            .Value = True
            .Interior.ColorIndex = 6
        End With
    End If
End If
End Sub
 
Upvote 0
Looking good. I modified the code as follows:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F10:F21")) Is Nothing Then
    If Target.Value = "Repair needed - non Roadable" Then
        With Range("F2")
            .Value = True
            .Interior.ColorIndex = 6
            .Value2 = "Flat Bed Truck Required"
        End With
    End If
End If
End Sub

When range F10:F21 cleared, F2 remains yellow with the text "Flat Bed Truck Required". How can we get F2 to be blank if range F10:F21 does not meet the criteria?
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F10:F21")) Is Nothing Then
    Range("F2").Clear
    If Target.Value = "Repair needed - non Roadable" Then
        With Range("F2")
            .Value = True
            .Interior.ColorIndex = 6
            .Value2 = "Flat Bed Truck Required"
        End With
    End If
End If
End Sub

Why Value2 and not Value?
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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