1
votes

I am using conditional formatting in Excel to compare two sets of data (set 1 in column A and set 2 in column D). The conditonal formatting is in column F so if column A=D then column F will turn green and if A /= D then column F will turn red. The problem is when I delete the first cells in A and D and shift all the cells below up, the whole conditional formatting gets erased. Is there a way to keep that conditonal formatting permanent?

[Im having trouble uploading pics for some reason...if I could, I would include a pic so it's easier to understand what's goin on]

2
This sounds like you are having trouble with the formatting in Excel, which is not a programming related question and therefore is off topic for this site. You may want to try SuperUserleigero
@leigero on the contrary, conditional formatting is another method of applying formulas to cells; while not as robust as other examples of programming, it is generally accepted as appropriate for this site (though it may also be appropriate for superuser.com).Grade 'Eh' Bacon
@Grade'Eh'Bacon Yes, but unless I'm missing something his problem has nothing to do with the conditional formatting. His problem is with losing his formatting when deleting and moving cells around in Excel which is a software problem and not a code problem.leigero
Thanks @Grade'Eh'Bacon. Yes, also this type of conditional formatting may be useful to you one day...it's common.Fen
@leigero Note my solution below - the correct result is achieved by changing the formulation of the conditional formatting rules - ie: by changing coding.Grade 'Eh' Bacon

2 Answers

0
votes

This is a general feature of Excel, not just of conditional formatting. In short, Excel tracks each cell as a unique object, and if it gets moved around (like a row is deleted), then it automatically shifts the reference to that object to refer to its new location.

Hypothetical Example

Consider a formula in cell F5, which says '=A5+D5'. If you rightclick on A3, delete that cell and shift cells up, the formula in F5 will now read '=A4+D5', because the old reference to A5 has now shifted upwards by 1.

You can theoretically solve this by using the INDIRECT function. In the example above, let's say you wanted to delete A3, and then you wanted the cell in F5 to still read '=A5+D5', even though the old A5 had shifted up. You could do this with the following formula:

=INDIRECT("A5")+D5

Because you are indirectly referring to A5 by explicitly writing it out as a text string, it won't move around when rows and columns change. Be warned that this is often counter intuitive. If you insert a new row above row 2, for example, your formula would now read:

=INDIRECT("A5")+D6

Note that D6 automatically shifted, but INDIRECT("A5") did not. This is likely not the desired result. However, if we assume that you will often be deleting (and shifting up) cells in column A, but you want all other worksheet changes to be incorporated, we can do what you want. Again ignoring your specific question for now, consider the above example. How can we change it so that when a whole new row is added, the formula ultimately becomes A6+D6? But at the same time, when A3 is deleted, have the formula now read A5+D5? In short, we do this by referencing the row that D5 is on, as follows:

=INDIRECT("A"&ROW(D5))+D5

Now, if a whole row is added, both instances of D5 in the above formula become D6. But if A3 is deleted, the "A" remains "A", and the D5 instances remain D5.

Actual Solution for your case

In your conditional formatting therefore, the formula would be as follows [Applied to all of column F, or however much of column F you want, starting with F1] [two rules one where A<>D and one where A=D; or, make formatting GREEN by default and have RED only apply where A<>D]:

=INDIRECT("A"&ROW(D1))<>D1
0
votes

The cause of this problem is likely that the cells you deleted were referred to in the conditional formatting syntax. For instance, if you have a conditional formatting, applied to cells F1:F5 with the formula =$A1=$D1, and then delete cells A1:A3 and D1:D3 (shifting cells up), you will notice that the conditional statement is now only applied to cells F3:F5. Excel tries to update these references for you, but occasionally that isn't what is desired.

Using a formula that doesn't directly refer to the cells will fix this. For instance:

=OFFSET($F1,0,-5)=OFFSET($F1,0,-2)

and

=OFFSET($F1,0,-5)<>OFFSET($F1,0,-2)

Now, when you delete any cells in columns A or D, the conditional formatting will still be applied to all rows.