条件格式在使用MATCH函数时不一致的问题

huangapple go评论99阅读模式
英文:

Conditional formatting inconsistent using MATCH in a custom formula

问题

我有两个单元格A1B1,它们包含相同的值。我想要使用自定义公式=MATCH(A1,B1)创建一个条件格式规则,使这两个单元格变成绿色。

这有时对其他单元格对起作用,但其他时候它根本不起作用。

请注意,这些单元格具有自定义数字格式,该格式在数字前添加了一个字母和一个分号。

请告诉我我做错了什么。

3对单元格,2对正常工作,Q3R3没有:
条件格式在使用MATCH函数时不一致的问题

我的条件格式设置:
条件格式在使用MATCH函数时不一致的问题

英文:

I have 2 cells A1 and B1 that contain the same values. I want to create a conditional formatting rule using the custom formula =MATCH(A1,B1) that colours both cells green.

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:
条件格式在使用MATCH函数时不一致的问题

My conditional formatting:
条件格式在使用MATCH函数时不一致的问题

答案1

得分: 1

RULE

在下面的公式中,MATCH 函数的 search_type 参数已更改为 0(精确匹配),其 range 参数现在通过将 search_key 列偏移 1-1 来计算,具体取决于它是其列对中的左列还是右列

# 应用到范围
M3:R55

# 自定义公式
=MATCH(M3,OFFSET(M3,0,IF(ISODD(COLUMN($M3))=ISODD(COLUMN()),1,-1)),0)

ISSUES

你的方法主要问题在于相对引用的处理方式。此外,匹配类型设置错误。

相对引用

你公式中的单元格引用是相对的。当将条件格式规则应用于一系列单元格时,相对引用会根据单元格在规则的 应用范围 中相对于第一个单元格(最顶部最左侧)的位置进行更新。

对公式的行引用进行偏移不会产生不良影响,但将列引用偏移奇数次数(每隔一次)会导致 MATCH 函数的 range 参数引用一个与预期完全不同的列对中的单元格。

通过将公式 =MATCH(M3,N3,0) 放置在 M4 中,然后将其复制到 N4O4 中,可以看到相同的行为。

  • N4 中,公式 =MATCH(N3,O3,0) 将无法按预期工作。它将尝试将来自左列的 N3 的值与来自完全不同对的右列中的 O3 的值进行匹配。
  • O4 中,公式 =MATCH(O3,P3,0) 的行为类似于 M4 中的原始公式。它将尝试将来自左列的 O3 的值与同一对的右列中的 P3 的值进行匹配。

search_type

原始公式中的 MATCH 函数将 search_type 参数设置为 1(默认),这指定在 range 中搜索小于或等于 search_key 的最大值,同时假定 range 按升序排列。应该将其设置为 0 以进行精确匹配。

原始公式在一半的时间内成功,但默认的 search_type 结合实际数据给人以错误的印象,即它在50%以上的情况下成功。在一些错误比较错误单元格的情况下,可能会应用预期的格式,而它们的值不匹配。

例如,将公式 M3 应用于 N3 时,它变为 =MATCH(N3,O3),这将在规则中返回 TRUE,因为 N3 > = O3

=MATCH(N3,O3)   // search_type: 1         
=MATCH(58,8)    // 
=1              // 58 >= 8 (8 matched in position 1)

=MATCH(N3,O3,0)   // search_type: 0
=MATCH(58,8,0)   
=#N/A             // 未找到匹配

EXPLANATION

条件格式规则应用于列对的连续范围,每对由左列和右列组成,例如 M3:R55

  1. 如果 MATCH 函数的 search_key 位于左列中,则其 range 参数应为 search_key 右侧一列的 OFFSET 单元格。
    • OFFSET(M3,0,1)
  2. 如果 search_key 在右列中,则 range 应为 search_key 左侧一列的 OFFSET 单元格。
    • OFFSET(M3,0,-1)
  3. 规则的 应用范围 中的第一列必定是左列。如果该列是奇数编号,则 所有 左列都将是奇数编号。如果它是偶数编号,则 所有 左列都将是偶数编号。
  4. 因此,可以通过 ISODD 函数对第一列进行测试,并对当前列进行相同的测试,如果当前列是左列,则这两个测试比较的结果必定都成功都失败
# 第一列是 M,当前单元格是 Q3

=ISODD(COLUMN($M3))=ISODD(COLUMN())
=ISODD(13)=ISODD(17)
=TRUE=TRUE
=TRUE        // 当前列是左列


# 第一列是 M,当前单元格是 R3

=ISODD(COLUMN($M3))=ISODD(COLUMN())
=ISODD(13)=ISODD(18)
=TRUE=FALSE
=FALSE      // 当前列是右列


# 第一列是 N,当前单元格是 R3

=ISODD(COLUMN($N3))=ISODD(COLUMN())
=ISODD(14)=ISODD(18)
=FALSE=FALSE
=TRUE       // 当前列是左列
  1. 唯一需要做的就是通过将 search_key 作为 OFFSET 函数的 cell_reference 传递,并根据 ISODD 测试的结果设置 offset_columns1-1,从而填充 MATCH 函数的 range 参数。
=MATCH(M3,OFFSET(M3,0,
  IF(ISODD(COLUMN($M3))=ISODD(COLUMN()),
    1,   // 条件为真(当前列是左列)
    -1)) // 条件为假(当前列是右列)
  ,0)
英文:

RULE

In the formula below, the MATCH function's search_type argument was changed to 0 (exact match) and it's range argument is now calculated by offsetting the search_key column by 1 or -1, depending if it is the left or right column in its column pair

# Apply to range
M3:R55

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

<br>

ISSUES

The main issue with your approach was tied to how relative refferences are handled. Additionally, the type of match was incorrectly set.

Relative References

The cell references in your formula are relative. When a conditional formatting rule is applied to a range of cells, relative references are updated for each cell based on its position in relation to the first cell (topmost-leftmost) in the rule's Apply to range.

Offsetting your formula's row references has no undesired effect, however, offsetting the column references by an odd number (every other) causes the MATCH function's range argument to reference a cell from a completely different column pair than was intended.

One can see the same behavior by placing the formula =MATCH(M3,N3,0) in M4 and then copying it to N4 and O4.

  • In N4 the formula =MATCH(N3,O3,0) will not work as intended. It will attempt to match a value from N3 which is in the right column of a pair, with a value from O3, which is in the left column of a completely different pair.
  • In O4 the formula =MATCH(O3,P3,0) will behave similarly to the original formula in M4. It will attempt to match the value from O3 which is in the left column of a pair, with a value from P3 which is in the right column of the same pair .

search_type

The original formula's MATCH function had the search_type argument set to 1 (default) which specifies searching the range for the largest value that is less than or equal to the search_key, while also assuming that the range is sorted in ascending order. It should have been set to 0 for an exact match.

The original formula was succeeding half the time, however the default search_type in combination with the actual data, was giving the false impression that it succeeding more than 50% of the time. The expected formatting would have been applied in some cases where the wrong cells were being compared, and where their values didn't match.

For example, the formula in M3 when applied to N3 becomes =MATCH(N3,O3) which would return TRUE in the rule because N3&gt;=O3

=MATCH(N3,O3)   // search_type: 1         
=MATCH(58,8)    // 
=1              // 58 &gt;= 8 (8 matched in position 1)

=MATCH(N3,O3,0)   // search_type: 0
=MATCH(58,8,0)   
=#N/A             // No Match found 

<br>

EXPLANATION

The conditional formatting rule is applied to a contiguous range of column pairs, with each pair comprised of a left and a right column, eg. M3:R55

  1. If the MATCH function's search_key is located in a left column, its range argument should be the cell OFFSET one column right of the search_key.
    • OFFSET(M3,0,1)
  2. If the search_key is in a right column, then the range should be the cell OFFSET one column left of the search_key
    • OFFSET(M3,0,-1)
  3. The first column in the rule's Apply to range is necessarily a left column. If that column is odd-numbered, all left columns will be odd-numbered. If it's even-numbered, all left columns will be even-numbered.
  4. Therefore, a test of the first column with the ISODD function, and the same test on the curent column, would necessarily both succeed or both fail if the current column is a left column.<br><br>
    For example,
# 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 populate the MATCH function's range argument by passing the search_key as the cell_reference of an OFFSET function, and setting offset_rows to 0, and offset_columns to 1 or -1 depending on the TRUE or FALSE outcome of the ISODD tests comparison:
=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)

[<img src="https://i.stack.imgur.com/rf3Ut.png" width="600" />](https://i.stack.imgur.com/rf3Ut.png "click to enlarge")

huangapple
  • 本文由 发表于 2023年6月1日 21:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定