1

我正在尝试创建一个过滤条件来检查数据,如果匹配,那么它将打印数据。我有以下内容,并且出现类型不匹配错误。对于 cTest。我还尝试了以下方法(不带引号 - 但它返回 false): cTest = (InStr(getName(iVar), a1) > 0)

有没有更简单的方法来创建过滤条件。

如果在标准中填写了字段 a1、a2 和 a3,则应在返回任何内容之前检查该值是否包含所有内容。

  Dim cTest As Variant

If a1 <> "" Or a2 <> "" Or a3 <> "" Then
If a1 <> "" Then
  If a2 <> "" Then
   If a3 <> "" Then
   'Value entered in 1,2,3
    cTest = "(InStr(getName(iVar), a1) > 0) And (InStr(getName(iVar), a2) > 0) And (InStr(getName(iVar), a3) > 0)"
    End If
    ' Value entered in 1,2
    cTest = "(InStr(getName(iVar), a1) > 0) And (InStr(getName(iVar), a2) > 0)"
      End If
      '(Value entered in 1 only
   cTest = "(InStr(getName(iVar), a1) > 0)"
ElseIf a2 <> "" Then
  If a3 <> "" Then
    'Value entered in 2,3
    MsgBox ("2nd, 3rd value entered")
  End If
   ' Value entered in 2 only
ElseIf a3 <> "" Then
    'Value entered in 3 only
     MsgBox ("3rd value entered")
Else
     ' no value entered
     MsgBox ("no value entered")
End If
End If

For iVar = 1 To Phrases.MaxIndex()


        If (cTest) Then
        'We want this one
        .Cells(irow, 1).Value = getName(iVar)
        irow = irow + 1
      End If
    Next iVar

  End With
4

1 回答 1

0

如何使用 AND & OR 条件的简单连接?

Sub testing()

    If ((a1 <> "" And InStr(getName(iVar), a1) > 0) Or a1 = "") And _
    ((a2 <> "" And InStr(getName(iVar), a2) > 0) Or a2 = "") And _
    ((a3 <> "" And InStr(getName(iVar), a2) > 0) Or a3 = "") Then
        ' the wanted one
    End If

End Sub
于 2012-10-10T09:44:29.203 回答