MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 9th, 2002, 07:16 PM   #1
andrew.ward
New Member
 
Join Date: May 2002
Location: New Zealand
Posts: 23
Default

Hi all,

I have formula as a text string. Is there a method of evaluating this equation directly in VBA rather than using string manipulation functions to extract the numerical values in order to create a new function?

That is, I have can obtain the equation of a trendline as a string. Can I evaluate this directly using some type of string evaluation function, and if so how? Or do I have to use the laborious string functions to extract the coefficients and recreate the function using variables?

E.G.

Dim x as double
Dim y as double
Dim TrendlineEquation as string

TrendlineEquation = “2.6667*x + 11.44”

x=2

y= SomeTypeOfEvaluationFunction (TrendlineEquation)

‘Or to put it another way:

y= SomeTypeOfEvaluationFunction (“2.6667*x + 11.44”)

‘Nb the value of ‘y’ should be 16.77

Thanks so much,
Andrew


[ This Message was edited by: andrew.ward on 2002-05-09 21:10 ]
andrew.ward is offline   Reply With Quote
Old May 9th, 2002, 08:37 PM   #2
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi Andrew.
I, with my limited knowledge of Excel's available functions, could not figure out a way to return a function from a string.
This could be done with a somewhat complicated user defined function.
So I think you may have answered yourself about using vaious string manipulations, ect to extract the values.
You might want to search the web for a function or wait until Aladin is browsing the board.
His functions generally give me a migraine, but they sure work.
Does your spreadsheet run in Office 2000?

Tom


_________________
Found a solution? If so, please post again so members of this board can spend their time helping others. Better still, edit your topic(intitial post), by tagging on a word or phrase such as, "Problem Solved", or "Resolved". Thanks for being courteous!

[ This Message was edited by: TsTom on 2002-05-09 19:41 ]
Tom Schreiner is offline   Reply With Quote
Old May 9th, 2002, 10:09 PM   #3
andrew.ward
New Member
 
Join Date: May 2002
Location: New Zealand
Posts: 23
Default

I solved the problem using various string manipulations. The resultant function obtains the equation that describes a trendline. Then it allows the user to input a value into that equation in order to get out an interpolated value based on the trendline equation.

Thanks for the help,

best regards,
Andrew

Function TrendLineValue(ByVal XValue As Double, ChartName As String, SeriesName As String) As Variant

'EXPLANATION
'The purpose of this function is to obtain values interpolated from existing data, using
'a trend line of the existing data. The parameters SeriesName and ChartName point
'this function to the series that has the trend line. It then obtains the equation that
'describes the trend line.

'The Function then uses XValue as the input value to the trend line equation. The
'resultant value of the trendline equation is the required interpolated value.

'Note: This function current only works for linear and power trendlines. The case statement
'is easy enough to modify if trendlines of other types are required.

'This function will fail if the specified series does not have a trend line


'PARAMETERS=============================================================================
'SeriesName: The name of the series that has the trendline, the equation of which is
'required.
'ChartName: The name of the chart that displays the series and associated trendline.
'XValue: The input value that will be put into the trendline equation in order to
'return the required value.

'VARIABLES=============================================================================
'TLEqn: The equation of the trendline
'TLEqnMod: The modified version of the trendline


'Variable Declarations=================================================================

Dim TLEqn As String
Dim TLEqnMod As String
Dim TLEqualPos, TLXPos, TLAddPos
Dim TLCoeff, TLExp, TLConst
Dim Area As Double
Dim TLType As Long

'BODY ==================================================================================

'Obtain the type of trendline that has been used
TLType = Charts(ChartName).SeriesCollection(SeriesName).Trendlines(1).Type

'Obtain the equation that describes the trendline
TLEqn = Charts(ChartName).SeriesCollection(SeriesName).Trendlines(1).DataLabel.Text

'remove the 'y' from the start of the trend line equation
TLEqnMod = Right(TLEqn, Len(TLEqn) - 1)

'remove spaces from the string

TLEqnMod = Trim(TLEqnMod)

'The case statement replaces the 'x' in the equation with a reference to a cell.
'Also, it turns the string into a proper equation based on the type of trend line that has
'been used.

Select Case TLType 'this is identifies the type of trendline

Case xlLinear

'find the position of =, x and + in the equation
TLXPos = InStr(1, TLEqnMod, "x", vbTextCompare)
TLEqualPos = InStr(1, TLEqnMod, "=", vbTextCompare)
TLAddPos = InStr(1, TLEqnMod, "+", vbTextCompare)

'Obtain the constants in the linear equation
TLCoeff = CDbl(Trim(Mid(TLEqnMod, TLEqualPos + 1, TLXPos - TLEqualPos - 1)))
TLConst = CDbl(Trim(Mid(TLEqnMod, TLAddPos + 1, Len(TLEqnMod) - TLAddPos)))


'Evaluate the Trend line equation using the user input and return the value
TrendLineValue = TLCoeff * XValue + TLConst

Exit Function

Case xlPower

'find the position of =, x and + in the equation
TLXPos = InStr(1, TLEqnMod, "x", vbTextCompare)
TLEqualPos = InStr(1, TLEqnMod, "=", vbTextCompare)

'Obtain the constants in the linear equation
TLCoeff = CDbl(Trim(Mid(TLEqnMod, TLEqualPos + 1, TLXPos - TLEqualPos - 1)))
TLExp = CDbl(Trim(Mid(TLEqnMod, TLXPos + 1, Len(TLEqnMod) - TLXPos)))


'Evaluate the Trend line equation using the user input and return the value
TrendLineValue = TLCoeff * XValue ^ TLExp

Exit Function

Case xlExponential, xlLogarithmic, xlMovingAvg, xlPolynomial

'This part needs to be modified if this function is to cope with the above
'types of trendlines.
TrendLineValue = "This type of trendline is not supported. Currently, only linear and power trendlines are supported"
Exit Function

Case Else ' Other values.

TrendLineValue = "This type of trendline is not supported. Currently, only linear and power trendlines are supported"
Exit Function

End Select

End Function


[ This Message was edited by: andrew.ward on 2002-05-09 21:11 ]

[ This Message was edited by: andrew.ward on 2002-05-12 14:44 ]
andrew.ward is offline   Reply With Quote
Old May 9th, 2002, 10:15 PM   #4
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Better make sure "Farty Fart Fart, Inc." wouldn't be pissed off that some of the code from your project for them is now freely available on the 'net. You may consider just removing the comment header.

It looks like nice code though. Well done.

_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-05-13 20:08 ]
Mark O'Brien is offline   Reply With Quote
Old May 9th, 2002, 10:23 PM   #5
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-05-09 18:16, andrew.ward wrote:
Hi all,

I have formula as a text string. Is there a method of evaluating this equation directly in VBA rather than using string manipulation functions to extract the numerical values in order to create a new function?

That is, I have can obtain the equation of a trendline as a string. Can I evaluate this directly using some type of string evaluation function, and if so how? Or do I have to use the laborious string functions to extract the coefficients and recreate the function using variables?

E.G.

Dim x as double
Dim y as double
Dim TrendlineEquation as string

TrendlineEquation = “2.6667*x + 11.44”

x=2

y= SomeTypeOfEvaluationFunction (TrendlineEquation)

‘Or to put it another way:

y= SomeTypeOfEvaluationFunction (“2.6667*x + 11.44”)

‘Nb the value of ‘y’ should be 16.77

Thanks so much,
Andrew


[ This Message was edited by: andrew.ward on 2002-05-09 21:10 ]
Hi Andrew...if I read you post correctly it
looks like your trying to evaluate the
Trend line eqaution from a chart Trend.

If so then try this;


Sub test()
Dim x As Double
Dim y As Double
Dim TrendlineEquation As String
Dim Rp As Double

Rp = 2
TrendlineEquation = "2.6667 * x + 11.44"
TrendlineEquation = Replace(TrendlineEquation, "x", Rp)

y = ExecuteExcel4Macro("Evaluate(" & TrendlineEquation & ")")
MsgBox y

End Sub




__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old May 12th, 2002, 03:57 PM   #6
andrew.ward
New Member
 
Join Date: May 2002
Location: New Zealand
Posts: 23
Default

Ivan,

you hit the nail on the head - I was looking for a command that allowed me to numerically evaluate a text string directly.

Thank you,

Andrew
andrew.ward is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 07:30 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes