首页 > 解决方案 > 行与列的比较(命名范围)

问题描述

我的数据:

表格1 人1 人2 人3 人4 人5 人6 人7 人8
周一 一种 C D F F
周二 F D C F 一种
周三 C D 一种 F F F
地方
一种
C
D

我的问题: 我想比较是否在每一天命名范围Place中的所有值都在行内Monday, Tuesday, Wednesday,...,如果有些缺少这些是哪一个。
所以,因为Wednesday它应该给出一个"B" missing公式IF或任何你有的建议。"E"并且"F"不在Place,那些应该被忽略。

到目前为止我尝试了什么: 研究总是让我比较 2 列而不是行和列。
我试过这样的数组函数($B3:$I3 == "Monday row" == "row1"):

=INDEX(Place;SMALL(IF(ISERROR(MATCH(Place;$B3:$I3;0));(ROW(Place)-MIN(ROW(Place))+1);"");ROWS($A$1:A1)))

错误:比较单个行需要许多“帮助”列。还有“#NUM!” 即使所有值都Place发生或仅缺少一个值。

我开始使用许多COUNTIF公式,例如

=COUNTIF(row1;INDEX(Place;1))+COUNTIF(row1;INDEX(Place;2))+COUNTIF(row1;INDEX(Place;3))+COUNTIF(row1;INDEX(Place;4))

当所有值都出现时给我一个"4",如果部分或全部丢失,则给我一个较低的值。但是我如何才能将这些数字与“看到”哪些值丢失导致“低于 4”数字联系起来呢?

标签: excelcomparison

解决方案


我认为这个 UDF 会给你你想要的结果。

Function Check(Data As Range) As String
    ' 296
    
    ' Data specifies one row from the table excl column #1
    Dim Fun()       As String               ' function return value
    Dim i           As Long                 ' index to Fun()
    Dim Places      As Variant              ' array of range "Place"
    Dim R           As Long                 ' loop counter; Places
    Dim Match       As Variant              ' result of Match() function
    
    ' this command will slow down your workbook.
    '   If inconvenient (too sluggish), remove it.
    ' If removed, the UDFs in the worksheet may not recalculate as you expect.
    '   Click "Calculate Now" on the ribbon's 'Formulas' tab to update.
    Application.Volatile
    
    ' the named range "Places" must exist in the worksheet
    ' in the sample it's B10:B13
    Places = ActiveSheet.Range("Places").Value
    ReDim Fun(UBound(Places))
    
    For R = 1 To UBound(Places)
        Match = Application.Match(Places(R, 1), Data, 0)
        If IsError(Match) Then
            i = i + 1
            Fun(i) = Places(R, 1)
        End If
    Next R
    
    If i Then
        Fun(0) = "Missing: "
    Else
        Fun(0) = "All present"
    End If
    ReDim Preserve Fun(i)
    Check = Replace(Join(Fun, ", "), ",", vbNullString, 1, 1)
End Function

代码必须粘贴到标准代码模块。您必须插入它,默认情况下它的名称类似于“Module1”,您可以更改它。不要使用任何现有的代码模块。通过输入类似于以下的语法,从工作簿中的任何位置调用该函数:=Check(C3:J3). 这里 C3:J3 定义了表的倒数第二列,不包括“星期一”,结果将根据 range 评估此 range 中列出的项目Places


推荐阅读