The code below calculates the formula contained in the Text (String) variable:
Private Sub CalculeFormulaNoTexto()
Dim Texto As String
Texto = "= 90 / 3 + 7 + COS(0) + 1/7"
' A variável Texto recebe a fórmula que quero calcular
' Observe que o primeiro caractere é o sinal de igual "='
' Sem ele o cálculo não ocorre
' Coloco o conteúdo de Texto na célula A1
Cells(1, 1) = Texto
'Pego o resultado do cálculo obtido na célula A1
Resultado = Cells(1, 1)
'Apresento o resultado
MsgBox Texto & " = " & Resultado
End Sub
Some remarks:
Check the contents of cell A1 on the worksheet , it has the formula the way it would look if it were typed in the cell itself
The variable " Result " can be used for other calculations because it stored the resulting value in the cell
PROBLEM
I did other tests and not all were successful
For example, to:
Texto = "= 9 + 2 * COS(3*PI()/2) + ARRED(1/7;4)"
An error will appear in the cell and the code will display an error as well (in this case it is not because of the ARRED function, use it in a smaller equation and it will work)
SURPRISE
If you go in the cell with error ("A1") and der F2 to edit and ENTER , the calculation will be done correctly
DOUBT
What is the reason for the error and how to solve this problem of executing a text within the VBA code?