Conditional Formatting VBA

cart0250

Active Member
Joined
Jun 24, 2006
Messages
287
Office Version
  1. 2016
Platform
  1. Windows
Good day,

I'm trying to apply conditional formatting to range B2:B500. If the cell value is NOT equal to "A" and If the cell is NOT blank, apply a gray interior (color index 15).
The below works, but only for the used range within the specified range. So if user subsequently adds data to one of the blank cells, the conditional formatting is not applied.

Can anyone help modify this so the conditional formatting will also be applied based on any new data the user enters into the blank cells?

I've been playing around with only applying the formatting if the value is not equal to "A" and if cell length is not equal to 0 (instead of blank)... but no luck yet.

Appreciate any help.


VBA Code:
Sub applyCF

Dim cond As FormatCondition
Dim rng As Range
Dim ws As worksheet

            Set rng = ws.Range("B2:B500").SpecialCells(xlCellTypeConstants)
            Set cond = rng.FormatConditions.Add(xlCellValue, xlNotEqual, "A")

            With cond
                .Interior.ColorIndex = 15
            End With
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Why not just apply the Conditional Formatting to every cell in your range, and just include logic in your rule in determining which cells to be turned gray.
If you use the "Formula" option of Conditional Formatting, your formula might look something like:
Excel Formula:
=AND(B2<>"A",B2<>"")
To get the VBA code for that, you can simply turn on your Macro Recorder and record yourself creating the Conditional Formatting rule (the Macro Recorder is a great tool to get snippets of coding like this!).
 
Upvote 0
Thanks... i was able to get it working as below.

VBA Code:
Sub applyCF

Dim rng As Range
Dim ws As worksheet

Set rng = ws.Range("B2:B500")           
  rng.FormatConditions.Add Type:=xlExpression, Formula1:= "=AND(B2<>""A"",B2<>"""")"
  rng.FormatConditions(1).StopIfTrue = False
  rng.FormatConditions(1).Interior.ColorIndex = 15

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,747
Messages
6,180,710
Members
452,994
Latest member
Janick

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