Assess Excel VBA Boolean Condition (Not Formula)

I have text in cell A1 as below

((True And False Or True) And (True And (Not True))) And (False Or True)

I need to evaluate this text and put the Boolean result (True / False) in another cell B1 using the VBA code .

I tried using the Evaluate function, it does not work

When I read this cell, it always returns a double quote string type enclosed in both directions. Therefore, it is treated as a string and does not evaluate a Boolean expression.

"((True And True Or True) And (True And (True))) And (True Or True)"

I want to write this path, but it does not work

If Range("A1").Value = True Then
    Range("B1").Value = True
Else
    Range("B1").Value = False
End If

I tried to save in a Boolean variable as well

Dim Result as Boolean
Result = CBool(Range("A1").Value) 

since this is a string, I get type mismatch when I tried to convert using CBool

Can anybody help?

early.

+4
3

, VBA .Evaluate:

Option Explicit

Public Sub TestMe()

    Dim cell01  As Range
    Dim cell02  As Range

    Set cell01 = Range("A1")
    Set cell02 = Range("A2")

    Range("A1") = "1+2+3+4+5"
    Range("A2") = "TRUE and FALSE"

    Debug.Print Evaluate(CStr(cell01))
    'Debug.Print CBool(cell02) - this will be an error!
    Debug.Print Evaluate(CBool("True") And CBool("False"))

    Debug.Print Evaluate("=AND(TRUE,FALSE)")
    Debug.Print Evaluate("=AND(TRUE,TRUE)")
    Debug.Print Evaluate("=OR(TRUE,TRUE)")

End Sub

TRUE and FALSE ( ), .

, TRUE and FALSE, =AND(TRUE,FALSE). VBA, Excel. , .

+1

- , Eval Access.

Public Function EvaluateExpression(Value As String) As Boolean
    With CreateObject("Access.Application")
        EvaluateExpression = .Eval(Value)
    End With
End Function

Public Sub T()
    Debug.Print EvaluateExpression("((True And True Or True) And (True And (True))) And (True Or True)")
End Sub

'True
+3

question :

Option Explicit

Sub Test()

    Debug.Print VBABooleanEvaluateOnTheFly("((True And False Or True) And (True And (Not True))) And (False Or True)")
    Debug.Print VBABooleanEvaluateOnTheFly("((True And True Or True) And (True And (True))) And (True Or True)")
    Debug.Print VBABooleanEvaluateOnTheFly("True")
    Debug.Print VBABooleanEvaluateOnTheFly("False")
    Debug.Print VBABooleanEvaluateOnTheFly("False Or True")

End Sub

Function VBABooleanEvaluateOnTheFly(strExpression As String) As Boolean

    Dim blnResult As Boolean
    Dim objVBComponent As Object

    Set objVBComponent = ThisWorkbook.VBProject.VBComponents.Add(1)
    With objVBComponent
        .CodeModule.AddFromString "Function foo() As Boolean: foo = " & strExpression & ": End Function"
        If Application.Run(.Name & ".foo") Then
            blnResult = True
        Else
            blnResult = False
        End If
    End With
    ThisWorkbook.VBProject.VBComponents.Remove objVBComponent

    VBABooleanEvaluateOnTheFly = blnResult

End Function

Trust access to the VBA project object model .

enter image description here

:

  • , .
  • it is vulnerable to code injection by an attacker, for example. they can enter something like Sheet1.Cells.Deleteinstead(True And False etc)
+3
source

Source: https://habr.com/ru/post/1689240/


All Articles