VBA Red Cell Criteria

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Good Day All,

In working with line 11 of my sheet, where I would like cell E11 to produce a highlighted red cell only if it is empty AND ONLY if the cells of that row specifically cells B11, C11, D11 & F11 are populated.

Likewise, if all of the cells of this row (row 11) of B11, C11, D11 & F11 are not populated, then cell E11 will not produce a highlighted red cell.

All of these cells have to be populated in order for the cell E11 to be highlighted, where if all of them are not populated then the code will not produce a highlighted red cell for E11.


Can you please help me create a vba code around this condition?

Thank you!
Pinaceous
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Could be done with conditional formatting :

=(E11="")*COUNTA(B11:C11,D11,F11)
 
Upvote 0
If you need a macro :
VBA Code:
Sub v()
[E11].Interior.ColorIndex = xlNone
If [E11] = "" And WorksheetFunction.CountA([B11:C11], [D11], [F11]) = 4 Then _
    [E11].Interior.ColorIndex = 3
End Sub
 
Upvote 0
CF formula:
Code:
=AND(E11="",COUNTA(B11:F11)=4)
or VBA code:
VBA Code:
Range("E11").Interior.Color = xlNone
If WorksheetFunction.CountA(Range("B11:F11")) = 4 And IsEmpty(Range("E11")) Then Range("E11").Interior.Color = vbRed
 
Upvote 0
Try this. Put under worksheet module for event trigger
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rngTrig As Range

Set rngTrig = Union(Range("B11,D11"), Range("F11"))
If Not Intersect(Target, rngTrig) Is Nothing Then
    If Application.WorksheetFunction.CountA(rngTrig) > 0 Then
        Range("E11").Interior.ColorIndex = 3
    Else
        Range("E11").Interior.ColorIndex = xlNone
    End If
End If

End Sub
 
Upvote 0
@Zot

1) E11 could be changed too. should be
VBA Code:
Set rngTrig = Range("B11:F11")

2) B to F11 could be cells with formula. Thus worksheet_Change could not fire it.
 
Upvote 0
@Zot

1) E11 could be changed too. should be
VBA Code:
Set rngTrig = Range("B11:F11")

2) B to F11 could be cells with formula. Thus worksheet_Change could not fire it.
We need more detail from OP :)
 
Upvote 0
Hello Gents'

The code is working well but I would like it to produce the interior color red cell for E11 only IF B11 & C11 & D11 and F11 have contents.

Based on this criteria, I am attaching a scenario to see if this can be produced.

Many thanks for providing your codes and input!
Pinaceous
 

Attachments

  • Capture.jpg
    Capture.jpg
    20.8 KB · Views: 4
Upvote 0
My understanding now is that when all B, C ,D and F not blank, not if any one is not blank.

I see that you want to implement on any row instead of just row 11. The code will count 0 is a content, not blank and will work on any row on the sheet for the same columns.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim n As Long
Dim rngTrig As Range

n = Target.Row
Set rngTrig = Union(Range("B" & n, "D" & n), Range("F" & n))
If Not Intersect(Target, rngTrig) Is Nothing Then
    Debug.Print Application.WorksheetFunction.CountA(rngTrig)
    If Application.WorksheetFunction.CountA(rngTrig) = 4 Then
        Range("E" & n).Interior.ColorIndex = 3
    Else
        Range("E" & n).Interior.ColorIndex = xlNone
    End If
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,159
Messages
6,123,351
Members
449,097
Latest member
thnirmitha

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