如果所有 Excel 列值不一致则返回

发布于 2024-10-21 02:17:30 字数 426 浏览 2 评论 0原文

我在 Excel 电子表格中有以下数据:

    A  B  C
 1  b  b  b

我想在数据后面的列中放置一个公式,该公式比较行中的每个(文本)值,如果它们全部相同,则返回 TRUE,即 A=B=C 。如果一个或多个值不一致,我希望它返回 FALSE。即

    A  B  C  D
 1  b  b  b  TRUE
 2  b  e  b  FALSE
 3  e  b  b  FALSE

我知道像 AND 这样的逻辑函数,因此可以构造像

AND(A1=B1,A1=C1.. etc) 这样的东西,但是,一旦列数增加,这很快就会变得难以处理增加。

重组数据是不可能的。

有人知道 OOTB / VB 解决方案吗?

I have the following data in an Excel Spreadsheet:

    A  B  C
 1  b  b  b

I would like to put a formula in the column after the data which compares each (text) value in the row, and returns TRUE if they're all the same ie A=B=C. I would like it to return FALSE if one or more of the values don't agree. ie

    A  B  C  D
 1  b  b  b  TRUE
 2  b  e  b  FALSE
 3  e  b  b  FALSE

I'm aware of logical functions like AND, hence could construct something like

AND(A1=B1,A1=C1.. etc), however, this soon gets unwieldy as soon as the number of columns increases.

Restructuring the data isn't do-able.

Does anyone know an OOTB / VB solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人心善变 2024-10-28 02:17:30

如果 countif 找到与 count 相同的数字,那么它们都是相同的。

=IF(COUNTIF(A1:C1,A1)=COUNTA(A1:C1),"true","false")

希望这就是您正在寻找的内容,您只需扩展要测试的任意多列的范围即可。

更新

正如评论中所指出的,如果数据集有空白单元格,则无法返回正确的结果。

即使范围内有空白单元格,这也会返回 false:

=IF(AND(COUNTIF(A1:C1;A1)=COUNTA(A1:C1);COUNTBLANK(A1:C1)=0);"true";"false")

If the countif finds the same number as the count then they are all the same.

=IF(COUNTIF(A1:C1,A1)=COUNTA(A1:C1),"true","false")

Hope this is what you are looking for, you just need to extend the ranges for however many columns you want to test.

Update

As pointed out in the comment this fails to return the right result if the dataset has blank cells.

This will return false even if there is a blank cell in the range:

=IF(AND(COUNTIF(A1:C1;A1)=COUNTA(A1:C1);COUNTBLANK(A1:C1)=0);"true";"false")
对你的占有欲 2024-10-28 02:17:30

这是一个 VBA UDF:要使其区分大小写,请删除选项比较文本

Option Explicit
Option Compare Text
Public Function AllSame(theRange As Range) As Boolean
    Dim vR As Variant
    Dim vC As Variant

    AllSame = True
    vR = theRange
    If IsArray(vR) Then
        vC = vR(1, 1)
        For Each vC In vR
            If vC <> vR(1, 1) Then
                AllSame = False
                Exit For
            End If
        Next vC
    End If
End Function

Here is a VBA UDF: to make it case sensitive remove Option Compare Text

Option Explicit
Option Compare Text
Public Function AllSame(theRange As Range) As Boolean
    Dim vR As Variant
    Dim vC As Variant

    AllSame = True
    vR = theRange
    If IsArray(vR) Then
        vC = vR(1, 1)
        For Each vC In vR
            If vC <> vR(1, 1) Then
                AllSame = False
                Exit For
            End If
        Next vC
    End If
End Function
诠释孤独 2024-10-28 02:17:30

虽然速度会慢一些,但您也可以使用数组公式(在单元格中输入后按Ctrl + Shift + Enter):

{=IF(SUM(COUNTIF(A1:C1,A1:C1))=POWER(COUNTA(A1:C1), 2),"true","false")}

即使范围内有空单元格,这也可以工作。

更新:
通过确保范围内只有 1 个唯一值,此数组公式也有效。即使范围内有空格,它仍然返回准确的值:

{=SUM(IFERROR(1 / COUNTIF(A2:C2,A2:C2), 0)) = 1}

Although it will be a bit slower, you can also use an array formula (by pressing Ctrl + Shift + Enter after typing it in the cell):

{=IF(SUM(COUNTIF(A1:C1,A1:C1))=POWER(COUNTA(A1:C1), 2),"true","false")}

This will work even if there are empty cells in the range.

.

UPDATE:
This array formula also works, by making sure that there's only 1 unique value in a range. It also still returns an accurate value, even if there are blanks in the range:

{=SUM(IFERROR(1 / COUNTIF(A2:C2,A2:C2), 0)) = 1}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文