I couldn't work out a way to do this with just built in worksheet functions, so I made a user defined function (UDF) called ContStates which returns the number of unique states associated with a given company.
Below, you don't need column I at all, it's just there to help me develop and to illustrate the formula working which is later used in conditional formatting.
Use it as follows:
=CountStates(G2,$G$2:$G$20,$F$2:$F$20)>2
where the first argument is the single company name, the second the full list of company names, and the third argument the corresponding statenames:
<embed src="https://www.box.net/embed/21hkj0ddxotas5c.swf" wmode="opaque" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" height="400" width="466">
You don't need to do this for your purposes, but to put it into conditional formatting, select the whole range of company names, selecting by starting at the top so that the active cell is in the topmost row, then using the
Use a Formula to… option (
Formula Is: in pre Exel 2007) and just paste in the whole formula, including the preceding equals sign:
<embed src="https://www.box.net/embed/vbl6nmd71cftsy3.swf" wmode="opaque" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" height="400" width="466">
and you get:
Excel Workbook |
---|
|
---|
| F | G | H | I |
---|
1 | State | Co name | | |
---|
2 | State1 | Co1 | | TRUE |
---|
3 | State1 | Co1 | | TRUE |
---|
4 | State3 | Co3 | | TRUE |
---|
5 | State4 | Co4 | | FALSE |
---|
6 | State5 | Co5 | | TRUE |
---|
7 | State1 | Co1 | | TRUE |
---|
8 | State7 | Co1 | | TRUE |
---|
9 | State8 | Co1 | | TRUE |
---|
10 | State4 | Co4 | | FALSE |
---|
11 | State1 | Co5 | | TRUE |
---|
12 | State1 | Co1 | | TRUE |
---|
13 | State3 | Co2 | | FALSE |
---|
14 | State4 | Co3 | | TRUE |
---|
15 | State5 | Co3 | | TRUE |
---|
16 | State6 | Co5 | | TRUE |
---|
17 | State1 | Co1 | | TRUE |
---|
18 | State8 | Co2 | | FALSE |
---|
19 | State9 | Co3 | | TRUE |
---|
20 | State4 | Co4 | | FALSE |
---|
|
---|
Now all this will fail unless it's backed up by the UDF itself in a standard code module:
Code:
Function CountStates(CoName, CoNames, StateNames)
Set xxx = CreateObject("Scripting.dictionary")
i = 0
For Each cll In CoNames.Cells
i = i + 1
If cll.Value = CoName.Value Then
If Not xxx.Exists(StateNames(i).Value) Then xxx.Add StateNames(i).Value, StateNames(i).Value
End If
Next cll
CountStates = xxx.Count
End Function