Convert Excel VBA string formula to real formula

SafetyHammer

New Member
Joined
Sep 4, 2016
Messages
2
Hi,

I have a code that copy the polynomial equation from a graph in excel,

y = -0.0008x3 + 0.0832x2 + 9.5105x - 0.7195 (formula keep changing depending on graph data)

I am trying to solve the formula for a given Y value. my problem is I am unable to convert it to a real formula in VBA so I can solve it

Code:
Sub CalcualteValveOPening()  Dim chrtobj As ChartObject
  Dim chrt As Chart
  Dim srs As Series
  Dim tline As Trendline
  Dim dlbl As DataLabel
  Dim str As String
  Dim z As Integer
  Dim x As Integer
  


  With ActiveSheet
    Set chrtobj = ActiveSheet.ChartObjects(1)
    Set chrt = chrtobj.Chart
    Set srs = chrt.SeriesCollection(1)
    Set tline = srs.Trendlines(1)
    Set dlbl = tline.DataLabel
    str = dlbl.Formula
    .Range("E27").Value = str
  End With
  
  

  For x = 0 To 100 Step 0.5
  z = Range("E27").Value
  If z / Range("E24").Value <= 1 Then Range("E29").Value = x
Next x


End Sub

Wondering if anyone can help with the conversion
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi and welcome to the MrExcel Message Board.

I would prefer to use the LinEst Worksheet Function to give you the numbers more directly.

For instance, this code evaluates the ranges specified and produced the polynomial coefficients up to third order and places them in a variant (v) which is set to an Array with all four values in it:
Code:
Sub Test()
    Dim v As Variant
    v = Application.LinEst(Range("B2:B14"), Application.Power(Range("A2:A14"), Array(1, 2, 3)))
End Sub

Regards,
 
Last edited:
Upvote 0
If your function has the form "y = f(x)" stored as string variable Fx

Then you can evaluate y for any given x with the following formula in VBA:

Code:
y = Evaluate(Replace(Right(Fx, Len(Fx) - 4), "x", x))

For example:

Code:
y = Evaluate(Replace(Right("y = x^2 + x + 1", Len("y = x^2 + x + 1") - 4), "x", 2))

Will give you y = 7 for x = 2
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,714
Members
449,118
Latest member
MichealRed

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