【问题标题】:Excel formula to compare single value in one cell with multiple values in other cellExcel公式将一个单元格中的单个值与另一个单元格中的多个值进行比较
【发布时间】:2014-08-22 12:57:12
【问题描述】:

我在 A 列中有一个值,我想将其与相应单元格列 B 的多个值进行比较,并将答案放在 C 列中。假设它在 B 列中搜索小于或等于 12 的值并放入C列中的答案顺序相同。

Column A ####  Column B        #### Column C

12       ####  10,12,13,14     #### Yes, Yes, No, No

101      ####  101,102,103,104 #### Yes, No, No, No

【问题讨论】:

  • 您究竟希望它在 A 列中返回什么?

标签: excel compare formula


【解决方案1】:

假设,如您在问题中所述,BC 列中的单元格是字符串,用逗号分隔,那么最简单的方法是使用用户定义的函数(称为 StringComp ) 在 VBA 中。

该函数有两个参数,一个用于Column B 中的逗号分隔数字列表,第二个指向您希望用作与列表比较的数字的单元格Column A

我认为这可以使用数组函数来完成,但是 VBA 解决方案要简单得多。

首先将以下 VBA 复制并粘贴到您的工作簿 VBA 模块中后,对于上面的示例,假设您的数据从 Row 1 开始,您可以通过将 =StringComp(B1,A1) 放入单元格 C1 来使用 StringComp 函数,根据您的示例,这将返回 Yes,Yes,No,No

Public Function StringComp(a As Variant, b As Variant) As String
Dim strOUT, strOUT_temp, strArray() As String
Dim intCount As Integer


strArray = Split(a.Text, ",")

For intCount = LBound(strArray) To UBound(strArray)
If (Val(strArray(intCount)) <= Val(b)) Then
    strOUT_temp = "Yes"
Else
    strOUT_temp = "No"
End If
If (intCount = LBound(strArray)) Then
     strOUT = strOUT_temp
Else
     strOUT = strOUT & "," & strOUT_temp
End If
Next

StringComp = strOUT

End Function

【讨论】:

  • 当时的答案是什么?或者答案在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
相关资源
最近更新 更多