简体   繁体   English

Excel条件格式设置问题

[英]Excel Conditional Formatting Probsss

I am using conditional formatting in Excel to compare two sets of data (set 1 in column A and set 2 in column D). 我在Excel中使用条件格式来比较两组数据(A列中的设置1和D列中的设置2)。 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. 条件格式位于F列中,因此,如果A = D列,则F列将变为绿色,如果A / = D则F列将变为红色。 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. 问题是,当我删除A和D中的第一个单元格并将所有单元格移到下方时,整个条件格式都会被删除。 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] [由于某些原因,我无法上传图片...如果可以的话,我会附上一张图片,以便更轻松地了解发生了什么]

This is a general feature of Excel, not just of conditional formatting. 这是Excel的常规功能,而不仅仅是条件格式。 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. 简而言之,Excel会将每个单元格作为一个唯一的对象进行跟踪,如果它移动了(就像删除了一行一样),则它会自动将对该对象的引用移动到该对象的新位置。

Hypothetical Example 假设的例子

Consider a formula in cell F5, which says '=A5+D5'. 考虑单元格F5中的公式,该公式为“ = 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. 如果右键单击A3,删除该单元格并向上移动单元格,F5中的公式现在将显示为'= A4 + D5',因为对A5的旧引用现在已向上移动了1。

You can theoretically solve this by using the INDIRECT function. 从理论上讲,您可以使用INDIRECT函数解决此问题。 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. 在上面的示例中,假设您要删除A3,然后您希望F5中的单元格仍然读取'= A5 + D5',即使旧的A5已向上移动。 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. 因为您是通过将A5显式地写为文本字符串来间接引用A5的,所以当行和列更改时,它不会四处移动。 Be warned that this is often counter intuitive. 请注意,这通常是违反直觉的。 If you insert a new row above row 2, for example, your formula would now read: 例如,如果您在第2行的上方插入新行,则公式现在将显示为:

=INDIRECT("A5")+D6

Note that D6 automatically shifted, but INDIRECT("A5") did not. 请注意,D6自动移动,但INDIRECT(“ A5”)没有移动。 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. 但是,如果我们假设您经常会删除(并向上移动)A列中的单元格,但是您希望合并所有其他工作表更改,那么我们可以做您想做的事情。 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? 我们如何更改它,以便在添加整个新行时,公式最终变为A6 + D6? But at the same time, when A3 is deleted, have the formula now read A5+D5? 但是同时删除A3时,公式现在是否显示为A5 + D5? In short, we do this by referencing the row that D5 is on, as follows: 简而言之,我们通过引用D5所在的行来做到这一点,如下所示:

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

Now, if a whole row is added, both instances of D5 in the above formula become D6. 现在,如果添加了整行,则上式中D5的两个实例都将变为D6。 But if A3 is deleted, the "A" remains "A", and the D5 instances remain D5. 但是,如果删除了A3,则“ A”仍为“ A”,而D5实例仍为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; 因此,在您的条件格式中,公式将如下所示[适用于F列的所有列,或所需的F列的大部分,以F1开头] [两个规则,其中A <> D,其中A = D; or, make formatting GREEN by default and have RED only apply where A<>D]: 或者,默认情况下将格式设置为绿色,而红色仅适用于A <> D]:

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

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 . 例如,如果您有条件格式设置,将其应用于公式=$A1=$D1单元格F1:F5 ,然后删除单元格A1:A3D1:D3 (向上移动单元格),则会注意到该条件语句现在仅应用于单元格F3:F5 Excel tries to update these references for you, but occasionally that isn't what is desired. Excel会尝试为您更新这些引用,但有时不是所需的。

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. 现在,当您删除列A或D中的任何单元格时,条件格式仍将应用于所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM