Excel如果满足条件,则查找并比较多列上的值

时间:2015-12-11 06:37:28

标签: arrays excel vba find next

我有一个包含代表客户的行的表,每个月,我在每一行上添加一列代表他们所做的订单类型(A,B或C)。

我需要识别每个订购A类订单的客户(在任何一个月内),然后在接下来的几个月中,制作一个C类订单。

然而,从" A"到了" C"键入顺序(一个或多个月做一个" C"),客户返回到" A",我不需要识别他。

" B"在找到" A"之后,订单无动于衷。为了

给出的例子:

Client  M1  M2  M3  M4  Identify
  01    A   B   C   C     Yes
  02    A   B   C   A      No
  03    A   C   B   A      No
  04    B   B   A   C     Yes
  05    B   A   C   B     Yes
  06    C   A   B   B      No
  07    A   A   A   C     Yes
  08    A   A   A   A      No
  09    A   C   A   C     Yes
  10    A   C   C   A      No

我不知道我是否可以使用简单的公式或使用VBA来实现。

EDITED:由于我每个月都会添加一个新列,公式或VBA应该考虑所有可能性(例如" ACCA"," ACCCCA"等等上)。还添加了客户端07,08,09和10作为示例。

提前谢谢。

JT

1 个答案:

答案 0 :(得分:2)

没有VBA 简单的方式是:(在F2然后复制下来)

=IF(ISERROR(FIND("AC",SUBSTITUTE(B2&C2&D2&E2,"B",""))),"No",IF(ISERROR(FIND("ACA",SUBSTITUTE(B2&C2&D2&E2,"B",""))),"Yes","No"))

作为" OnlyDoesWhatIAskedFor" -UDF把它放在模块中:

Public Function getOrder(rng As Range) As String
  Dim runner As Variant, i As Long, comb As Variant
  comb = Array("A", "C", "A")
  getOrder = "No"
  For Each runner In rng.Value
    If runner = comb(i) Then
      If i = 2 Then getOrder = "No": Exit Function
      If i = 1 Then getOrder = "Yes"
      i = i + 1
    ElseIf runner = comb(0) Then
      i = 1
    ElseIf runner <> "B" Then
      i = 0
    End If
  Next
End Function

然后在单元格中输出(假设F2并简单地向下复制)

=getOrder(B2:E2)

它只检查范围内的A-C顺序,而B将被忽略,A-C-A也输出&#34;否&#34; ...

修改

如果只有最后的AC / ACA计数,那么你需要这样的东西:(在模块中)

Public Function getResult(rng As Range) As String
  Dim a As Variant, b As String
  'Col W - changes the cells to one long string / "B" will be left out
  For Each a In rng.Value
    If a <> "B" Then b = b & a
  Next
  'Col X - skips xxACAxx to Axx till no ACA is left
  While InStr(b, "ACA")
    b = Mid(b, InStr(b, "ACA") + 2)
  Wend
  '<~~~ add from here for ACCA = "No" (ACCAC will still be "YES")
  While InStr(b, "ACCA")
    b = Mid(b, InStr(b, "ACCA") + 2)
  Wend
  '<~~~ add till here for ACCA = "No" (ACCAC will still be "YES")
  'Col Y - Checks if an AC is found in the leftover
  'Col Z - if the check is >0 then it will be YES else No
  If InStr(b, "AC") Then getResult = "Yes" Else getResult = "No"
End Function

您可以在下图中看到它执行的每个步骤: enter image description here