You really don't need VBA to do this.
Keep in mind that you cannot achieve the desired behavior with a single conditional formatting rule; You will need to have a separate rule for each line color definition based on sales. Secondly: I found that achieving the desired conditional formatting behavior in Excel is much simpler using Named Ranges for rules instead of regular formulas.
Given these issues, follow these steps to create your named range, and then create your conditional formatting rules.
- First, select the first sales cell on the sheet (top row)
- Then give the cell the name SALE. Do this by pressing Ctl + F3 or select
Formulas β Name Manager on the ribbon. Then select New.. In Name: enter SALES and in Refers to: enter =$XN , where X is the column of the first sales cell and N is the line number. Press Enter . - Now select the entire range of cells that you want to develop.
- Choose
Home β Conditional Formatting β New Rule... - Select
Use a Formula to Determine Which Cells to Format and enter =SALES=number , where number is the number of sales that you want to call in color. - Select
Format and the Fill tab. Now you need to decide what background color you want for your selected sales number. You can also select other formatting options, such as font color, etc. - Click OK, OK, OK. Repeat steps 3 through 6 for each selected sales / color combination. If you want the color "all sales to be less than X", in your rule you enter
=SALES<number (<is "less"; you can also make <=, which is "less than OR equal to"), if you want the rule happened between two numbers, you can do =AND(SALES<=CEILING, SALES>=FLOOR) , where the ceiling and floor are the upper and lower boundaries. If you need a color for "all sales, big X", you can do =SALES>number .
EDIT:
To simplify the entry of conditional formulas, you can use the Stop, if there is truth function. Go to Home β Conditional Formatting β Manage Rules , and select This Worksheet from the drop-down menu. Now you will see a list of all the rules that apply to your sheet, and to the right of each rule there will be a checkbox "Stop, if it is right."
For each line color rule, select the Stop if true check box. Now your formulas can be like this (for example):
=Sales>25 for the green rule=Sales>10 for the yellow rule=Sales>0 for the Red rule
Etc, instead:
=AND(Sales>0,Sales<=10) for the Red rule=AND(Sales>10,Sales<=25) for the yellow rule=Sales>25 for the green rule
The Stop If True box means that once a formatting rule has been applied to a cell, that cell will not be formatted again based on any other rules that apply to it. Note that this means that the order of the rules matters when using Stop If True .
Rick teachey
source share