Conditional Formatting using VBA

Michael515

Board Regular
Joined
Jul 10, 2014
Messages
136
Hi,

I would like to write a conditional format in VBA that does the following:

If any cell in M="True" highlight the entire row in a light blue shade and change the color to a dark blue shade (ex. $M2="True")

I would like to apply the rule to the entire column M.

I hope that was clear enough, thanks ahead of time for the help :)
 
The condition should be =$M2="TRUE" with the quotation, however I have had problems typing that into VBA, any advice?
Note that TRUE could actually be two different things in Excel - there is the text entry "TRUE" and there is the boolean entry TRUE.
How do you know which one you have? Boolean entries are centered within the cell, by default, and text entries are left-justified in the cell, by default.
You could also check with an equation like this on some blank cell on your sheet.
Code:
=M2=TRUE
If the word TRUE appears in M2, and that formula returns TRUE, then it is boolean. If it returns FALSE, then it is text.

Why is that important? When referring to text values, you need to use Text qualifiers (double-quotes). When referring to boolean values, you don't.
So if your data is boolean, then the way the formula is currently written is fine:
Code:
Formula1:="=$M2=TRUE"
If it is Text we need to add text qualifiers. But since text qualifiers are used in creating the formula, it can get a little messy. I like to use CHR(34), which is the ASCII representation of a double-quote, when trying to write literal double-quotes into a formula in VBA.
So I would probably create that formula in a string variable like this:
' Create formula for text entry TRUE
Code:
    Dim myFormula As String
    myFormula = "=$M2=" & Chr(34) & "TRUE" & Chr(34)
and then use it in your Conditional Formatting line like this:
Code:
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:=[COLOR=#ff0000]myFormula[/COLOR]
The second problem is having the rule apply to the entire row if cell M# is TRUE, any suggestions for that?
Note how your formula is being applied to "Selection". That means it is applied to whatever range is selected when the code is run (normally, I don't like to leave it to chance and have my VBA figure out exactly which range to apply the code to). So, if you select complete rows instead of just column M, it will be applied to the whole row.

The key to getting it to work correctly is the way the range is referenced in your formula. Usually, the row and column references move as the formula moves across different cells in your range. That is, unless you lock them down with an absolute range reference. By placing a "$" in front of the row/column, it locks that part of the range down.
In your formula, it is front of the "M". That is what we want. As we apply it to different columns within our row, we want that reference to be "frozen" to column M. However, we do not want it in front of the row reference, as we want that reference to change rows as we move down rows.
See here for more on range references: Relative And Absolute Range References

To dynamically select the range to apply this to (all rows with data in column M, starting on row 2), you can put this block before your first row of code:
Code:
    Dim lastRow As Long
    Dim myRange As Range
    
'   Find last row in column M with data
    lastRow = Cells(Rows.Count, "M").End(xlUp).Row
    
'   Set range to be whole rows from row 2 down to last row
    Set myRange = Rows("2:" & lastRow)

'   Apply conditional formatting to defined range
    myRange.Select
 
Last edited:
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Thank you again Joe, you have really gone above and beyond the call of duty in helping me get this all figured out! I can't thank you enough and I can say that this thread has been one of the most informational and helpful to me since I've joined. Again I really appreciate you helping me figure out my code and teaching me along the way . . . Keep it up!
 
Upvote 0
You are welcome!
Glad I was able to help out!:)
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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