VBA conditioanl formatting a range

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
Aside from the excel conditioanl formatting is there a VBA script that I can run that will check a large range of cells. If a cell in the range equals TRUE then that cell is changed to a red background. If a cell in the range equals FALSE then nothing.

The only reason this needs to be a VBA script is because I only want this change to occur only when the user clicks a button not all the time.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
With a button from the Controls toolbar try

Code:
Private Sub CommandButton1_Click()
Dim c As Range
Me.UsedRange.Interior.ColorIndex = xlNone
For Each c In Me.UsedRange
    If c.Value = True Then c.Interior.Color = vbRed
Next c
End Sub
 
Upvote 0
Does it have to be a button? If you use a checkbox you could do it with conditional formatting by using the checkbox control cell as criteria.

Basic formula structure would be something like

=and(your criteria, control cell = true)
 
Upvote 0
With

Code:
Private Sub CommandButton1_Click()
Dim c As Range
Me.UsedRange.Interior.ColorIndex = xlNone
For Each c In Me.UsedRange
    If c.Value = True Then c.Interior.Color = vbRed
Next c
End Sub</pre>

How do I set the range this script looks at to something else like: AG167:AG407
 
Upvote 0
Maybe I should word this a different way. The range of cells I want to run this script on is:
Code:
Range("AG167:AG407")
If a cell in the range equals TRUE that cell is changed to a red background. If a cell in the range equals FALSE then do nothing at all and keep the original bgcolor.

Is this possible using the above code?
 
Upvote 0
Haven't tested this,

Code:
Private Sub CommandButton1_Click()
Dim c As Range
Me.Range("AG167:AG407").Interior.ColorIndex = xlNone
For Each c In Me.Range("AG167:AG407")
If c.Value = True Then c.Interior.Color = vbRed
Next c
End Sub

but it looks right.
 
Upvote 0

Forum statistics

Threads
1,223,975
Messages
6,175,749
Members
452,667
Latest member
vanessavalentino83

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