在if条件下使用whereclause

时间:2018-11-28 08:02:00

标签: excel vba excel-vba

我有5个字符串参数A,B,C,D,E,我想将它们用作IF语句中的条件。

我还有5个局部变量,以匹配参数

问题是,我不知道它们是否为空字符串。如果它是空字符串,我不希望在if条件中使用它们

Private Function XX(A As String, B As String, C As String, D As String, E As String) As Long

Dim AA As String
Dim BB As String
Dim CC As String
Dim DD As String
Dim EE As String

If A = AA && B = BB && C = CC & D = DD & E = EE Then
.....
End If

我知道sql中的一个子句可以做到,例如:

Dim Whereclause As String
IF B is NULL Then
whereclause = " WHERE AA = " + A + "AND C = " + C + "AND D = " + 
D ....

但是似乎我不能将其放入IF语句中,因为查询本身是一个字符串。

在IF条件下仍然可以使用吗?我不需要连接数据库,只想检查具有匹配所有参数的行的excel工作表中的记录

非常感谢您的帮助~~

1 个答案:

答案 0 :(得分:0)

您可以使用辅助功能

Function OkParameter(par As String, locVar As String) As Boolean
    OkParameter = IIf(par = "", True, par = locVar)
End Function

要包含在您的代码中,如下所示:

Private Function XX(A As String, B As String, C As String, D As String, E As String) As Long

    Dim AA As String
    Dim BB As String
    Dim CC As String
    Dim DD As String
    Dim EE As String

    If OkParameter(A, AA) And OkParameter(B, BB) And OkParameter(C, CC) And OkParameter(D, DD) And OkParameter(E, EE) Then

    End If
End Sub