“if”语句的逻辑测试是否可以是excel vba中的变量?

“if”语句的逻辑测试是否可以是excel vba中的变量?,excel,variables,if-statement,vba,Excel,Variables,If Statement,Vba,我有以下功能: Function get_equal_array_subset(column_label As String, _ loop_array() As Variant, _ values_array() As Variant) ' this function outputs an arr

我有以下功能:

Function get_equal_array_subset(column_label As String, _
                                                loop_array() As Variant, _
                                                values_array() As Variant)
    ' this function outputs an array of value from the values_array, based on a loop through the loop_array
    ' column_label is the first item in the array of the ouput array; i.e. the column lable of a new range
    ' loop_array is array being looped through and testing each value
    ' valus_array is the array from which values are taken with the test is met in the first array
    ' *** arrays have to be of equal lenght ***


    Dim subset_array() As Variant
    subset_array = Array(column_label)

    Dim rows_dim As Long
    Dim cols_dim As Integer

    Dim agent_subset_counter As Long
    agent_subset_counter = 0 ' counter to set the key for the new array

    For rows_dim = 2 To UBound(loop_array, 1)
        For cols_dim = 1 To UBound(loop_array, 2)

                If loop_array(rows_dim, cols_dim) > 2 Then
                        agent_subset_counter = agent_subset_counter + 1 '  increase the subset counter by 1
                        ReDim Preserve subset_array(agent_subset_counter) ' resize the array account for the next id
                        subset_array(agent_subset_counter) = values_array(rows_dim, cols_dim) ' add the new id to the agent subset
                End If
        Next cols_dim
    Next rows_dim
    get_equal_array_subset = subset_array
End Function

有没有办法让
If循环数组(rows\u dim,cols\u dim)>2然后
成为一个变量?假设我希望测试是
>3
=5
非空的
…等等。

如果你想将“幻数”2变成一个变量,那么如果你想要单独的逻辑,你可以使用一个数组项来代替2

,然后使用Select Case结构。

我会选择
应用程序类的magic
应用程序。Evaluate()
方法。例如,可以将一系列测试定义到一个数组中,例如:

Dim myTests(4)
myTests(1) = "> 3"
myTests(2) = "= 5"
myTests(3) = "+3 < 5"
myTests(4) = "- 4 + sum(1,2) < 5" 
显然,变量
j
应该根据您想要使用的测试来定义,这种方法允许您定义多个运算符数组(一个数组用于
+
-
等运算符,另一个数组用于
3
5
等值)

注意如果您还不知道,则
应用程序.Evaluate()
方法将对表达式求值,并像Excel那样返回结果。基本上,它使用的代码与Excel用于评估单元格中所写内容的代码相同:

Application.Evaluate("2+3") --> 5
Application.Evaluate("2 < 3") --> True
Application.Evaluate("IF(2=3,1,2)") --> 2
'etc.
Application.Evaluate(“2+3”)-->5
Application.Evaluate(“2<3”)-->True
应用程序。评估(“如果(2=3,1,2)”)-->2
等等。

因此,选择案例值可以是变量?您可以创建一个驱动选择案例逻辑的变量。
Application.Evaluate("2+3") --> 5
Application.Evaluate("2 < 3") --> True
Application.Evaluate("IF(2=3,1,2)") --> 2
'etc.