3
The code below calculates the formula contained in the variable Text (String):
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 spreadsheet, she has the formula the way it would be if it was typed in the cell itself
The variable "Upshot" can be used for other calculations as it has stored the resulting value that is in the cell
PROBLEM
I’ve had other tests and not everyone has succeeded
For example, for:
Texto = "= 9 + 2 * COS(3*PI()/2) + ARRED(1/7;4)"
There will be an error in the cell and the code will be error too (in this case it is not because of the ARRED function, use it in a smaller equation and it will work)
SURPRISE
If you are in the error cell ("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?
Worked, grateful.
– Leo
Not at all. If the answer was helpful, please consider accepting it. ;)
– Luiz Vieira
Is accepted not?
– Leo
Yes, now it is. :)
– Luiz Vieira
I just wanted to thank Luiz Vieira for having answered the question above, THANK YOU, I was already going crazy
– Ivan Costa