0

我正在尝试实现以下目标:向用户显示一个 Excel 电子表格,其中包含他们可以更改的假设列表。

Title       |     Value        |
Input01     |       10         |  =
Input02     |       2          |  >=
Input03     |       800        |  >=
Input04     |       4          |  >=
Input05     |       2          |  <=

If .. Then如果满足假设,则有一个声明会提取数据。但是,如果假设是空白的,则不应将其包含在If .. Then声明中。

If x = Input01Value And y >= Input02Value _
And z >= Input03Value And a >= Input04Value _
And b <= Input05Value Then

用户省略 Input03

If x = Input01Value And y >= Input02Value _
And a >= Input04Value And b <= Input05Value Then

现在我可以检查每个值是否存在,然后在它后面加上另一个If带有适当变量的语句。但这似乎有点多余。

我想知道是否可能出现以下情况:

Input 01 = ""
If Input01Value != "" Then Input01 = "x = " & Input01Value
'Then use join or something similar to join all of them ..

然后Input01If .. Then语句中直接使用这个。这样,当变量为空时,And ..不包括在内,并且If语句不会失败。
例如。(我知道这不起作用,只是说明场景)

VBA: If Input01 Then
Result while compiling: If x = Input01Value Then

请注意,我知道我可以执行以下操作:
If Boolean And Variable2 > 4 Then然后在单元格中拥有BooleanVariable2填充一个值,但是这样做的问题是,例如,如果用户决定省略Variable2(这是合理的)它将失败。例如。If (Boolean = True) And > 4 Then.

希望我的问题很清楚,感谢您的帮助。

4

2 回答 2

1

使用一个根据字符串运算符和两个值对选择案例进行操作的函数怎么样?

Function conditionalString(condition As String, x As Variant, y As Variant) As Boolean
Select Case condition
    Case "="
        If (x = y) Then
            conditionalString = True
        Else
            conditionalString = False
        End If
        Exit Function
    Case ">="
        conditionalString = (x >= y)
        Exit Function
    Case "<="
        conditionalString = (x <= y)
        Exit Function
    Case ">"
        conditionalString = (x > y)
        Exit Function
    Case "<"
        conditionalString = (x < y)
        Exit Function
    Case Else
        conditionalString = False
End Select
End Function

然后,您可以只使用另一个函数,在调用所有假设之前说“检查值是否不为空”。

于 2013-08-07T18:26:09.547 回答
1

扩展我的评论,您可以使用类似这样的方法来测试每一行输入。

Function TestIt(testValue,inputOperator,inputValue) As Boolean
    If Len(inputValue)=0 Then
        TestIt=True 'ignore this test: no value supplied
    Else
        TestIt=Application.Evaluate(testValue & inputOperator & inputValue)
    End If
End function
于 2013-08-07T18:41:54.347 回答