简体   繁体   English

在自定义公式中使用 MATCH 的条件格式不一致

[英]Conditional formatting inconsistent using MATCH in a custom formula

I have 2 cells A1 and B1 that contain the same values.我有 2 个包含相同值的单元格A1B1 I want to create a conditional formatting rule using the custom formula =MATCH(A1,B1) that colours both cells green.我想使用自定义公式=MATCH(A1,B1)创建条件格式规则,将两个单元格都染成绿色。

This sometimes works on other cell pairs but other times it simply doesn't work.这有时适用于其他细胞对,但有时它根本不起作用。

Note that the cells have a custom number format that adds a letter and a semi-colon before the numbers.请注意,单元格具有自定义数字格式,在数字前添加一个字母和一个分号。

Let me know what I am doing wrong.让我知道我做错了什么。

3 pairs, 2 pairs worked, Q3 & R3 didn't: 3 对,2 对起作用, Q3R3没有: 3 对电池对,2 对电池对起作用,但右边的最后一个没有。

My conditional formatting:我的条件格式:这是我的条件格式

Relative References相关参考

The cell references in your rule are relative.规则中的单元格引用是相对的。 This means that when the current cell is in a different row or column, the formula shifts accordingly.这意味着当当前单元格位于不同的行或列时,公式会相应地移动。 Changes in the current row don't affect your rule, but changes in the current column number, that are not that in multiples of 2, mean the MATCH function's range argument uses the wrong column.当前行的更改不会影响您的规则,但当前列号的更改不是 2 的倍数,这意味着MATCH函数的range参数使用了错误的列。

You can see the behavior by putting =MATCH(Q3,R3,0) in Q4 and then copy it to R4 .您可以通过将=MATCH(Q3,R3,0)放在Q4中然后将其复制到R4来查看行为。 The search_key argument will automatically update to R3 and the range argument to S3 . search_key参数将自动更新为R3range参数为S3 The new formula will be =MATCH(R3,S3,0) , but you need the range argument to be Q3 because R3 is in a right column: =MATCH(R3,Q3,0) .新公式将为=MATCH(R3,S3,0) ,但您需要range参数为Q3 ,因为R3在右列中: =MATCH(R3,Q3,0) This same thing is taking place in your conditional formatting rule.同样的事情也发生在您的条件格式规则中。

Corrected Rule更正规则

The following custom formula will work as expected.以下自定义公式将按预期工作。 It adjusts based on whether the current cell is the left, or the right column, of a column pair.它根据当前单元格是列对的左列还是右列进行调整。

"Apply to range" “应用于范围”

M3:R55

"Custom formula is" “自定义公式是”

=MATCH(M3,OFFSET(M3,0,IF(ISODD(COLUMN($M3))=ISODD(COLUMN()),1,-1)),0)

Explanation解释

  1. You have a contiguous range of column pairs, M3:R55 , where each pair is comprised of a left and a right column您有一系列连续的列对M3:R55 ,其中每对由左列和右列组成
  2. If the MATCH function's search_key argument is from a left-column cell, then its range argument needs to be the cell OFFSET one column to the right of the search_key .如果MATCH函数的search_key参数来自左列单元格,则其range参数需要是search_key右侧一列的单元格OFFSET
    • OFFSET(M3,0,1)
  3. If the MATCH function's search_key argument is from a right-column cell, then its range argument needs to be the cell OFFSET one column to the left of the search_key .如果MATCH函数的search_key参数来自右列单元格,则其range参数需要是search_key左侧一列的单元格OFFSET
    • OFFSET(M3,0,-1)
  4. The very first column in the range is necessarily a left column.范围中的第一列必然是左列。 If that column is odd-numbered, all left columns will be odd-numbered.如果该列是奇数,则所有左列都将是奇数。 If that column is even-numbered, all left columns will be even-numbered.如果该列是偶数,则所有左侧列都将是偶数。
  5. Therefore, one test applying the ISODD function to the first column's number, and a second identical test on the current column, would necessarily both succeed or both fail if the current column is a left column.因此,如果当前列是左列,则将ISODD function 应用于第一列编号的测试和对当前列进行的第二个相同测试必然都成功都失败

Examples例子

# First Column is M, current cell is Q3

=ISODD(COLUMN($M3))=ISODD(COLUMN())
=ISODD(13)=ISODD(17)
=TRUE=TRUE
=TRUE        // current col is left


# First Column is M, current cell is R3

=ISODD(COLUMN($M3))=ISODD(COLUMN())
=ISODD(13)=ISODD(18)
=TRUE=FALSE
=FALSE      // current col is right


# First Column is N, current cell is R3

=ISODD(COLUMN($N3))=ISODD(COLUMN())
=ISODD(14)=ISODD(18)
=FALSE=FALSE
=TRUE       // current col is left
  1. All that remains is to build the MATCH formula and use a comparison of the ISODD tests to determine whether to use 1 or -1 for the OFFSET function's range argument:剩下的就是构建MATCH公式并使用ISODD测试的比较来确定是对OFFSET函数的range参数使用1还是-1
=MATCH(M3,OFFSET(M3,0,
  IF(ISODD(COLUMN($M3))=ISODD(COLUMN()),
    1,   // condition_true (current col is left)
    -1)) // condition_false (current col is right)
  ,0)

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

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