A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » New Users
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Evaluating formula in VBA



 
 
Thread Tools Display Modes
  #1  
Old June 11th, 2009, 03:38 PM posted to microsoft.public.excel.newusers
Walter Briscoe
external usenet poster
 
Posts: 62
Default Evaluating formula in VBA

I have a string variable containing a formula which I want to evaluate.
I can do what I want with
activeCell.FormulaLocal = fx
result = activeCell.Value

I want a function which evaluates result without writing to a cell.
i.e. I am looking for the name of a function which will do
result = name(fx) and return the value of fx.

fx might have a value like "=""prefix"" & A1 & ""suffix""".

I have failed to find such a function. ;(
--
Walter Briscoe
  #2  
Old June 11th, 2009, 04:04 PM posted to microsoft.public.excel.newusers
Gary''s Student
external usenet poster
 
Posts: 7,584
Default Evaluating formula in VBA

Sub eval()
Dim s As String
s = "=A1+A2"
MsgBox (Evaluate(s))
End Sub

--
Gary''s Student - gsnu200856


"Walter Briscoe" wrote:

I have a string variable containing a formula which I want to evaluate.
I can do what I want with
activeCell.FormulaLocal = fx
result = activeCell.Value

I want a function which evaluates result without writing to a cell.
i.e. I am looking for the name of a function which will do
result = name(fx) and return the value of fx.

fx might have a value like "=""prefix"" & A1 & ""suffix""".

I have failed to find such a function. ;(
--
Walter Briscoe

  #3  
Old June 11th, 2009, 05:09 PM posted to microsoft.public.excel.newusers
Walter Briscoe
external usenet poster
 
Posts: 62
Default Evaluating formula in VBA

In message of Thu,
11 Jun 2009 08:04:02 in microsoft.public.excel.newusers, Gary''s Student
writes
Sub eval()
Dim s As String
s = "=A1+A2"
MsgBox (Evaluate(s))
End Sub


I had looked at the Excel 2003 VBA help, where I found:
Evaluate Method
See AlsoApplies ToExampleSpecificsConverts a Microsoft Excel name to an
object or a value.

expression.Evaluate(Name)
expression Optional for Application, required for Chart, DialogSheet,
and Worksheet. An expression that returns an object in the Applies To
list.

Name Required String. The name of the object, using the naming
convention of Microsoft Excel.

....

It did not seem to meet my purposes.

I guess I hit a documentation bug. ;(
Thank's for the prompt, effective help.
--
Walter Briscoe
  #4  
Old June 12th, 2009, 06:06 AM posted to microsoft.public.excel.newusers
Shane Devenshire[_3_]
external usenet poster
 
Posts: 3,333
Default Evaluating formula in VBA

Hi,

A little more detail would help - if this formula were "prefix" & A1
"suffix" then if A1 was 27 the answer would be prefix27suffix.

Is that really what you want?


--
If this helps, please click the Yes button.

Cheers,
Shane Devenshire


"Walter Briscoe" wrote:

I have a string variable containing a formula which I want to evaluate.
I can do what I want with
activeCell.FormulaLocal = fx
result = activeCell.Value

I want a function which evaluates result without writing to a cell.
i.e. I am looking for the name of a function which will do
result = name(fx) and return the value of fx.

fx might have a value like "=""prefix"" & A1 & ""suffix""".

I have failed to find such a function. ;(
--
Walter Briscoe

  #5  
Old June 12th, 2009, 08:32 AM posted to microsoft.public.excel.newusers
Walter Briscoe
external usenet poster
 
Posts: 62
Default Evaluating formula in VBA

In message of Thu,
11 Jun 2009 22:06:01 in microsoft.public.excel.newusers, Shane
Devenshire writes
Hi,

A little more detail would help - if this formula were "prefix" & A1
"suffix" then if A1 was 27 the answer would be prefix27suffix.

Is that really what you want?



Thank you for being willing to help.

Yes! Gary''s Student pointed me at Evaluate() in another posting and has
completely answered the question from my perspective.

To put the question in context:
I have a cell which contains a hyperlink call. I want to extract the
link location, evaluate it and use it in the following pseudo-code:
set IE - CreateObject("InternetExplorer.Application")
IE.Navigate2 evaluation
Write data extracted from IE document to other cells.

If my cell is "=hyperlink(""prefix"" & a1 & ""suffix"", ""name"")",
I construct a formula to evaluate "prefix" & a1 & "suffix".
I did not know about the evaluate() function.
I was misled by VBA F1 - help.
--
Walter Briscoe
  #6  
Old July 1st, 2009, 10:33 AM posted to microsoft.public.excel.newusers
Walter Briscoe
external usenet poster
 
Posts: 62
Default Evaluating formula in VBA

In message of Fri, 12 Jun 2009
08:32:16 in microsoft.public.excel.newusers, Walter Briscoe
writes
In message of Thu,
11 Jun 2009 22:06:01 in microsoft.public.excel.newusers, Shane
Devenshire writes


[snip]

To put the question in context:
I have a cell which contains a hyperlink call. I want to extract the
link location, evaluate it and use it in the following pseudo-code:
set IE - CreateObject("InternetExplorer.Application")
IE.Navigate2 evaluation
Write data extracted from IE document to other cells.

If my cell is "=hyperlink(""prefix"" & a1 & ""suffix"", ""name"")",
I construct a formula to evaluate "prefix" & a1 & "suffix".
I did not know about the evaluate() function.
I was misled by VBA F1 - help.


I tried to use evaluate and got an error 2015.
I left dealing with this error for some time while I did other things.
(I could write to a cell and read the result in place of evaluate.)
Yesterday I found there is a 255 character evaluate input string limit.

My string was of the form "=""rhubarb rhubarb""&a5&""...
I calculated "rhubarb rhubarb" & evaluate("=a5&""...)
I now have code which does what I want.

Is there any technique/are there any techniques for turning error codes,
such as 2015 from evaluate above, into text?
--
Walter Briscoe
 




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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 08:17 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.