如何索引匹配单元格中的条件集

How to index match a condition set in a cell

我试图通过索引匹配 table 来避免使用多个 if 公式,但是我需要匹配的是实际条件和字符串。

查找table:

+---+------------------+-------------------+-------+
|   |        A         |         B         |   C   |
+---+------------------+-------------------+-------+
| 1 | Current to Prior | Portfolio Comment | Error |
| 2 | =0               | ""                | 1     |
| 3 | <>0              | ""                | -1    |
| 4 | >0               | OK – Losses       | 0     |
| 5 | <0               | OK – Losses       | 1     |
| 6 | <0               | OK – New Sales    | 0     |
| 7 | >0               | OK – New Sales    | 1     |
+---+------------------+-------------------+-------+

具有正确硬编码输出的数据示例(C 列):

+---+------------------+-------------------+-------+
|   |        A         |         B         |   C   |
+---+------------------+-------------------+-------+
| 1 | Current to Prior | Portfolio comment | Error |
| 2 | 0                |                   | 1     |
| 3 | -100             | OK – Losses       | 1     |
| 4 | 50               |                   | -1    |
| 5 | 200              | OK – Losses       | 0     |
| 6 | 0                |                   | 1     |
| 7 | -400             | OK – New Sales    | 0     |
| 8 | 0                |                   | 1     |
+---+------------------+-------------------+-------+

我需要一个公式,将数据值与查找条件、数据字符串与查找字符串相匹配,并输出 return 值。

我知道你不一定要 VBA 解决方案,但我自己(和许多其他人)更喜欢使用 UDF,因为在我看来,它使阅读公式更容易和更清晰 - 而且你可以没有辅助细胞。

我们通过创建 Select 案例陈述来启动您的 UDF。对于案例,我们可以选择使用数值或字符串。我决定使用字符串。

在每种情况下,您将比较提供给 lngCondition 参数的数值,最终 return 函数的值。

由于您没有任何关于文本值何时可以具有 lngCondition = 0 的情况,我将其设为 return 工作表错误代码 #VALUE,正如您所期望的来自任何其他 Excel 公式。这就是 UDF 具有变体 return 类型的原因。

Public Function ReturnErrorCode(lngCondition As Long, strComment As String) As Variant

    Select Case strComment
    Case ""
        If lngCondition = 0 Then
            ReturnErrorCode = 1
        Else
            ReturnErrorCode = -1
        End If

    Case "OK - Losses"
        If lngCondition > 0 Then
            ReturnErrorCode = 0
        ElseIf lngCondition < 0 Then
            ReturnErrorCode = 1
        Else
            ' Your conditions don't specify that 'OK - Losses'
            '     can have a 0 value
            ReturnErrorCode = CVErr(xlErrValue)
        End If

    Case "OK - New Sales"
        If lngCondition < 0 Then
            ReturnErrorCode = 0
        ElseIf lngCondition > 0 Then
            ReturnErrorCode = 1
        Else
            ' Your conditions don't specify that 'OK - New Sales'
            '     can have a 0 value
            ReturnErrorCode = CVErr(xlErrValue)
        End If

    Case Else
        ReturnErrorCode = CVErr(xlErrValue)

    End Select

End Function

然后您将在工作表中使用此公式:

=ReturnErrorCode(A1, B1)


Great! But I have no knowledge of VBA and don't know how to add a UDF.

  • 首先,您需要打开 VBA 编辑器。您可以同时按下 Alt + F11.
  • 接下来,您需要创建一个标准代码模块。在 VBE 中,单击 Insert,然后单击 select Module (不是 Class 模块!).
  • 然后复制上面的代码,粘贴到刚刚创建的新代码模块中。
  • 由于您现在已经将 VBA 代码添加到您的工作簿中,因此您现在需要在下次保存时将其另存为 启用宏的工作簿