color a cell based on 2 conditions

StartingOut

Board Regular
Joined
Feb 1, 2011
Messages
92
Hi, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I have a worksheet with several columns and 500 rows, I need a VBA script or formula that will check one of 3 conditions<o:p></o:p>
<o:p></o:p>
the cells are B3 H3 N3 the key cell is N3<o:p></o:p>
<o:p></o:p>
1- if N3 contains the word ball then check the conditions of B3 and H3 and proceed to set 2 and 3. if not, then do nothing<o:p></o:p>
<o:p></o:p>
2- If N3 contains the word "ball" and B3 contains "P1" and H3 is "=<2:00:00" then set H3 cell fill color to green; but if H3 is >2:00:00 set H3 fill color to Red<o:p></o:p>
<o:p></o:p>
3- If N3 contains the word "ball" and B3 contains "P2" and H3 is "=<6:00:00" then set H3 cell fill color to green; but if H3 is >6:00:00 set H3 fill color to Red
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I have a worksheet with several columns and 500 rows, I need a VBA script or formula that will check one of 3 conditions<o:p></o:p>
<o:p></o:p>
the cells are B3 H3 N3 the key cell is N3<o:p></o:p>
<o:p></o:p>
1- if N3 contains the word ball then check the conditions of B3 and H3 and proceed to set 2 and 3. if not, then do nothing<o:p></o:p>
<o:p></o:p>
2- If N3 contains the word "ball" and B3 contains "P1" and H3 is "=<2:00:00" then set H3 cell fill color to green; but if H3 is >2:00:00 set H3 fill color to Red<o:p></o:p>
<o:p></o:p>
3- If N3 contains the word "ball" and B3 contains "P2" and H3 is "=<6:00:00" then set H3 cell fill color to green; but if H3 is >6:00:00 set H3 fill color to Red


Does this work?

Code:
Sub StartingOut()

If Range("N3").Value = "ball" Then

    Select Case Range("B3").Value
    
    Case Is = "P1"

        Select Case Range("H3").Value
        
        Case Is <= #2:00:00 AM#
        
                Range("H3").Interior.ColorIndex = 4
                
        Case Is > #2:00:00 AM#
        
                Range("H3").Interior.ColorIndex = 3
                
        End Select
        
    Case Is = "P2"
    
        Select Case Range("H3").Value
        
        Case Is <= #6:00:00 AM#
        
                Range("H3").Interior.ColorIndex = 4
                
        Case Is > #6:00:00 AM#
        
                Range("H3").Interior.ColorIndex = 3
                
        End Select

    End Select
        
End If

End Sub
 
Upvote 0
Thats great, it does work.

but 2 things, I need it to check all the rows, right now it works only in row 3. also the time checking of <= 2:00:00 is not quite right when the time is 2:00:00 it should be green but it is coding it as red
 
Upvote 0
This can be done with conditional formatting...

Highlight H3
Click format - conditional formatting

Condition 1:
Select Formula Is
=AND(N3="ball",OR(AND(B3="P1",H3<="2:00:00"+0),AND(B3="P2",H3<="6:00:00"+0)))
Format for Green

Click Add
Condition 2:
Select Formula is
=AND(N3="ball",OR(AND(B3="P1",H3>"2:00:00"+0),AND(B3="P2",H3>"6:00:00"+0)))
Format for Red


Hope that helps.
 
Upvote 0
Thats great, it does work.

but 2 things, I need it to check all the rows, right now it works only in row 3. also the time checking of <= 2:00:00 is not quite right when the time is 2:00:00 it should be green but it is coding it as red

Try this change:

Code:
Sub StartingOut()

Dim i As Long

Dim lr As Long

lr = Cells(Rows.Count, 1).End(3).Row

For i = lr To 2 Step -1

If Range("N" & i).Value = "ball" Then

    Select Case Range("B" & i).Value
    
    Case Is = "P1"

        Select Case Range("H" & i).Value
        
        Case Is = #2:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 4

        Case Is < #2:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 4
            
        Case Is > #2:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 3
                
        End Select
        
    Case Is = "P2"
    
        Select Case Range("H" & i).Value
        
        Case Is <= #6:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 4
                
        Case Is > #6:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 3
                
        End Select

    End Select
        
End If

Next i

End Sub
 
Upvote 0
Excelent!! with one last little thing, if for some reason when column N, that once read Ball and the cel in columb H was color filled, if column N Ball is changed to something else the color is not removed from the cell in column H when the code executes
 
Upvote 0
Change

Code:
End If
to
Code:
Else
    Range("H" & i).Interior.ColorIndex = xlNone
End If


Hope that helps.
 
Upvote 0
well done!!! it all works like a charm, Thank You so much.

If you could indulge me just this one last time, lets say I wanted this code to ony execute from row 3 to row 1000 so that my header rows from 1 to 2 keep their color
 
Upvote 0
well done!!! it all works like a charm, Thank You so much.

If you could indulge me just this one last time, lets say I wanted this code to ony execute from row 3 to row 1000 so that my header rows from 1 to 2 keep their color

You're welcome. I hope this works.

Code:
Sub StartingOut()

Dim i As Long

Dim lr As Long

lr = Cells(Rows.Count, 1).End(3).Row

For i = lr To 3 Step -1

If Range("N" & i).Value = "ball" Then

    Select Case Range("B" & i).Value
    
    Case Is = "P1"

        Select Case Range("H" & i).Value
        
        Case Is = #2:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 4

        Case Is < #2:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 4
            
        Case Is > #2:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 3
                
        End Select
        
    Case Is = "P2"
    
        Select Case Range("H" & i).Value
        
        Case Is <= #6:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 4
                
        Case Is > #6:00:00 AM#
        
                Range("H" & i).Interior.ColorIndex = 3
                
        End Select

    End Select
        
Else

    Range("H" & i).Interior.ColorIndex = xlNone

End If

Next i

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,486
Members
452,917
Latest member
MrsMSalt

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