Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 9th, 2011, 03:38 PM
Authorized User
Join Date: May 2009
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default IF statement in conditional formatting

Has anyone tried using an IF statement within conditional formatting? If so, I would like to know if it worked, and if so, was it straight forward or were there special issues to be dealt with, and how you set it up. I very much do not want to have to try to put it into VBA as it would be nightmarish to get it into all the cells and keep it working correctly.

I would like to use a formula something like the following in the conditional formatting formula.

IF(cell above = x, format a, format b).

Or better yet, I would like to use a nested IF statement. Something like:
IF(cell above = x, format a, IF(cell below=y,format b,format c))

I don't see how this can be done in the manage rules box of Excel 2007, but if it can be done, someone on this site knows how.
Old May 9th, 2011, 11:10 PM
Friend of Wrox
Join Date: Sep 2010
Posts: 171
Thanks: 0
Thanked 14 Times in 14 Posts

hey robert,
select the cell you want to conditionally format, then on the home tab, click on "conditional formatting," then on "manage rules."

Now click on "New rule," then on "use a formula to determine which cells to format"

Add your condition that should be applied last. So something like "=cell below= y". Whatever your formulas are, they need to evaluate to TRUE or FALSE.

Then select the formatting you want applied, hit ok.

Then do the exact same thing for "cell above," i.e. hit "New rule,", then "use a formula..."

Your "format C" is your default in the equation you gave as the example, so you shouldn't need to enter a formula for that, just set that as the format for the cell.

You might have some logic issues you'll have to resolve. What if, in your formula,

"IF(cell above = x, format a, IF(cell below=y,format b,format c))"

both cell above is x AND cell below = y?

You can move the order the rules get applied with the two arrows to the right of "delete rule." Also, don't forget you can use the And() and Or() functions in each/both of the rules if that will make the logic work to format the cells as you need. Just make sure that whatever you put in each rule as a formula, it can only evaluate to TRUE or FALSE.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional formatting r05579 SharePoint Development 0 February 19th, 2010 12:08 PM
conditional formatting jeremy1048 Access 4 July 3rd, 2008 11:20 AM
Conditional formatting vemaju Excel VBA 2 June 6th, 2007 04:41 AM
Conditional Formatting Corey Access 1 November 8th, 2005 12:18 AM
Conditional Formatting... In Access??? lguzman Access VBA 2 September 23rd, 2004 03:52 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.