How to calculate convert string into formula and calculate in vb

wedelia

New Member
Joined
Sep 27, 2007
Messages
12
Hi all,

I'm setting up a spreadsheet at the moment and would like to offer the users to input the equation (for example: "=(Log10((delta / 324.33) ^ (1 / -0.0451)))") that was derived from other sources in Excel.

*delta is a range of values

I'm trying to get vb to get the equation and calculate using the equation for different delta values.

What I have done is:

Static Function XXValue(byVal delta As Single, byVal Type As String)

Dim UserEquation As String
Dim UserDefinedEqn As Single

If Type = "user-defined" Then

UserEquation = Range("UserdefinedEqn").Value
'UserdefinedEqn is the name for cell containing the equation that user has input

CalculatedEqn = Evaluate(UserEquation)

If CalculatedEqn > 2 Then
No = CalculatedEqn
If Calculated Eqn > Range("Limit").Value Then
'Limit the name for cell containing the limit of the calculation
No = Range("Limit").Value
End If

Number = 10 ^ No

End If

XXValue = Number

End Function

Not sure where I have gone wrong in this set of code. It doesn't seem to calculate at all. Could someone please offer some advice?

Thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Yeap picking up the formula string from a cell.

I have tried to pick up the formula string from a cell with variables (x) in the formula. I have attempted to define x in the function but it was not recognised when i did evaluate (formula).
 
Upvote 0
Select A1.
Type "x+2" (no quotes) and press Enter
Run this Macro

Code:
Sub Test()
    Dim formulaString as String
    Dim uiValue as Double

    formulaString = Range("A1").Value

    Do
        uiValue = Application.InputBox("Enter the value to be evaluated", type:=1)
        If uiValue = 0 Then Exit Sub: Rem cancel pressed

        MsgBox "The value of " & formulaString & " when x = " & uiValue _
            & vbCr & "= " & EvaluateFormula(formulaString, "x", uiValue)
    Loop Until False
End Sub

Function EvaluateFormula(formulaStr, ParamArray Variables_Values() As Variant) As Variant
    Dim i As Long
    
    For i = 0 To UBound(Variables_Values) Step 2
        formulaStr = Replace(formulaStr, Variables_Values(i), Variables_Values(i + 1))
    Next i
    
    EvaluateFormula = Evaluate(formulaStr)
    If IsError(EvaluateFormula) Then EvaluateFormula = formulaStr
End Function
 
Last edited:
Upvote 0
Hi Mike,

I had followed your instructions and enter x+2 in cell A1. The macro does not pick up the formula string. I did a msgbox to see what it has picked up from formulaString = Range("A1").Value . The msgbox gives me duplicate - script file name instead of x+2.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top