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 evalu
Further to the accepted answer for this question you can use this code:
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
You will need to tick the Trust access to the VBA project object model checkbox in the Trust Center settings.
Just to note a couple of things with this technique:
Sheet1.Cells.Delete instead of (True And False etc)