I have text in a 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 Boolean result (True / False) in another cell B1 using VBA code.
I tried to use Evaluate function, it is not working.
When I read this cell, it always return string type with double quote enclose in both side. Hence it is treated as string and not evaluating the Boolean expression.
"((True And True Or True) And (True And (True))) And (True Or True)"
I want to write this way, but it is not working
If Range("A1").Value = True Then
Range("B1").Value = True
Else
Range("B1").Value = False
End If
I tried to store in Boolean variable also
Dim Result as Boolean
Result = CBool(Range("A1").Value)
as it is string, I am getting type mismatch when I tried to convert using CBool.
You could try something like this, taking advantage of the Eval function in 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With