Excel vba变量逻辑运算符

时间:2015-04-02 12:57:58

标签: excel vba excel-vba

我有一个电子表格,用户可以在其中定义一些规则,但我很难让日志工作。我希望根据用户的需求得出不同的结果,但理想情况下,我希望远离必须但多个选择案例或者如果行。

我拥有的是什么(简化 - 它在一个循环中,因此每行的值都会改变 - 我只是使用一个例子):

操作员可以变化,可以是">"," =>"等

Field1Value = "Football"
Operator1 = "="
Rule1 = "Football"

If Evaluate(Field1Value & Operator1 & Rule1 ) Then

'Run My Code

End If

所以在上面的例子中逻辑是真的,我的代码会运行,但是我遇到了类型不匹配的错误。

有没有人有任何想法?

由于 克里斯

******** ******** UPDATE

以下是完整的代码:

Workbooks(MasterWB).Activate

Sheets("Rules").Select

    NoRules = Sheets("Rules").Range("J6").End(xlDown).Row

    For a = 7 To NoRules

        Field1 = Workbooks(DataWB).Sheets(DataWS).Rows(1).Find(Workbooks(MasterWB).Sheets("Rules").Cells(a, 10), , xlValues, xlWhole).Column
        Operator1 = Sheets("Rules").Cells(a, 11)
        Rule1 = Sheets("Rules").Cells(a, 12)

        Operator = Sheets("Rules").Range("J5").Cells(a, 13)

        Field2 = Workbooks(DataWB).Sheets(DataWS).Rows(1).Find(Workbooks(MasterWB).Sheets("Rules").Cells(a, 14), , xlValues, xlWhole).Column
        Operator2 = Sheets("Rules").Cells(a, 15)
        Rule2 = Sheets("Rules").Cells(a, 16)

        HighlightColumn = Workbooks(DataWB).Sheets(DataWS).Rows(1).Find(Workbooks(MasterWB).Sheets("Rules").Cells(a, 17), , xlValues, xlWhole).Column
        HighlightColour = Workbooks(MasterWB).Sheets("Rules").Cells(a, 17).Interior.ColorIndex

        Workbooks(DataWB).Activate

        With Workbooks(DataWB).Sheets(DataWS)

            .Select

            Lastrow = .UsedRange.Rows.Count

            For b = 2 To Lastrow

            Field1Value = .Cells(b, Field1).Value

                If Evaluate(Field1Value & Operator1 & Rule1) Then

                        .Cells(b, HighlightColumn).Interior.ColorIndex = HighlightColour                      

                End If

            Next b

        End With


    Next a

3 个答案:

答案 0 :(得分:2)

假设你期待"足球" ="足球"收益率True,然后尝试:

If Evaluate("""" & Field1Value & """ " & Operator1 & " """ & Rule1 & """") Then

注意,我将我的字符串括在引号中 - 你需要将它们传递给Evaluate()

实际上,根据我对MS Docs的阅读,我认为这不会起作用。

评估(名称

  

名称:使用Microsoft Excel的命名约定的公式或对象的名称。名称的长度必须小于或等于255个字符。

"足球&#34 ;?的定义是什么?它是一个简单的文本字符串吗?我的阅读表明Evaluate()将执行内置函数或UDF,但不是简单的比较。

答案 1 :(得分:2)

坚持我并不是要从FreeMan那里窃取信用,但是后来为我工作了:

Sub test()
Dim Field1Value As String
Dim Operator1 As String
Dim Rule1 As String

Dim test As Range
Dim Passed As Boolean

    Field1Value = "Football"
    Operator1 = "="
    Rule1 = "Football"

    Passed = Evaluate("=" & """" & Field1Value & """" & Operator1 & """" & Rule1 & """")

    'Different way of achievieng same thing ignore this if you want to 
    Set test = Range("A1")
    test.Formula = "=" & """" & Field1Value & """" & Operator1 & """" & Rule1 & """"
    Passed = test.Value

    MsgBox Passed
End Sub

我尝试了几个值和运算符,它运行正常。

所以我只会通过添加"=" &作为评估的第一部分来纠正FreeMan。

所以为了让这个更简洁,我说写这个:

Evaluate("=" & """" & Field1Value & """" & Operator1 & """" & Rule1 & """")

答案 2 :(得分:0)

尝试以下内容:

Field1Value = "Football"
Operator1 = "="
Rule1 = "Football"

Dim x As Variant

strEvaluate = Chr(34) & Field1Value & Chr(34) & Operator1 & Chr(34) & Rule1 & Chr(34)

x = Evaluate("IF(" & strEvaluate & ",1,0)")

if x = 1 then
'Run your code
end if

或者代替10使用TRUEFALSE。我的语言不是英语。但我认为这也有效。

x = Evaluate("IF(" & strEvaluate & "," & True & ", " & False & ")")

if x = TRUE then
'Run your code
end if