Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Evaluating (Splitting out) a formula

Hi,

If there was a formula in cell A1 of the following: =offset(C1,1,1) +
G1 * sheet2!A1

I would like to (using VBA) determine the number of elements in the
formula. So for the above formula the evaluating would be:

Parts: 3 (1=offset(C1,1,1), 2 = G1, 3=sheet2!A1)
Elements: 5 (C1, 1, 1, G1, sheet2!A1)

Ref Elements: 3 (C1, G1, sheet2!A1)
Number Elements: 2 (1,1)

Any help would much be appreciate. (Apologies for posting half
complete beforehand)

Best

Meldrum

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Evaluating (Splitting out) a formula

You simply need to count the number of commas and add one. I made an
unviersqal UDF whiich will look for any character that is passed to the
function and returns one more than the count. I set CountString to one
rather than 0 at the beginning to give a results one more than the count.

Call with
=CountString(A1,",")


Function CountString(TargetString As String, _
FindChar As String) As Integer

'Start a one because number of strings is 1 more
'than the number of times a character appears
CountString = 1
For i = 1 To Len(TargetString)
If Mid(TargetString, i, 1) = FindChar Then
CountString = CountString + 1
End If
Next i

End Function


"meldrum_scotland" wrote:

Hi,

If there was a formula in cell A1 of the following: =offset(C1,1,1) +
G1 * sheet2!A1

I would like to (using VBA) determine the number of elements in the
formula. So for the above formula the evaluating would be:

Parts: 3 (1=offset(C1,1,1), 2 = G1, 3=sheet2!A1)
Elements: 5 (C1, 1, 1, G1, sheet2!A1)

Ref Elements: 3 (C1, G1, sheet2!A1)
Number Elements: 2 (1,1)

Any help would much be appreciate. (Apologies for posting half
complete beforehand)

Best

Meldrum


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Evaluating (Splitting out) a formula

offset(C1,1,1) has two commas, the OP wants this to count once!

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Joel" wrote in message
...
You simply need to count the number of commas and add one. I made an
unviersqal UDF whiich will look for any character that is passed to the
function and returns one more than the count. I set CountString to one
rather than 0 at the beginning to give a results one more than the count.

Call with
=CountString(A1,",")


Function CountString(TargetString As String, _
FindChar As String) As Integer

'Start a one because number of strings is 1 more
'than the number of times a character appears
CountString = 1
For i = 1 To Len(TargetString)
If Mid(TargetString, i, 1) = FindChar Then
CountString = CountString + 1
End If
Next i

End Function


"meldrum_scotland" wrote:

Hi,

If there was a formula in cell A1 of the following: =offset(C1,1,1) +
G1 * sheet2!A1

I would like to (using VBA) determine the number of elements in the
formula. So for the above formula the evaluating would be:

Parts: 3 (1=offset(C1,1,1), 2 = G1, 3=sheet2!A1)
Elements: 5 (C1, 1, 1, G1, sheet2!A1)

Ref Elements: 3 (C1, G1, sheet2!A1)
Number Elements: 2 (1,1)

Any help would much be appreciate. (Apologies for posting half
complete beforehand)

Best

Meldrum




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Evaluating (Splitting out) a formula

What you need to do is parse the equation. This is something done by
compilers or interpreters.

The coding of a compiler can be a bit tricky. I suggest you start by geting
a good book on compiler construction.
--
Gary''s Student - gsnu2007k


"meldrum_scotland" wrote:

Hi,

If there was a formula in cell A1 of the following: =offset(C1,1,1) +
G1 * sheet2!A1

I would like to (using VBA) determine the number of elements in the
formula. So for the above formula the evaluating would be:

Parts: 3 (1=offset(C1,1,1), 2 = G1, 3=sheet2!A1)
Elements: 5 (C1, 1, 1, G1, sheet2!A1)

Ref Elements: 3 (C1, G1, sheet2!A1)
Number Elements: 2 (1,1)

Any help would much be appreciate. (Apologies for posting half
complete beforehand)

Best

Meldrum


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Evaluating (Splitting out) a formula

On Sun, 31 Aug 2008 02:55:35 -0700 (PDT), meldrum_scotland
wrote:

Hi,

If there was a formula in cell A1 of the following: =offset(C1,1,1) +
G1 * sheet2!A1

I would like to (using VBA) determine the number of elements in the
formula. So for the above formula the evaluating would be:

Parts: 3 (1=offset(C1,1,1), 2 = G1, 3=sheet2!A1)
Elements: 5 (C1, 1, 1, G1, sheet2!A1)

Ref Elements: 3 (C1, G1, sheet2!A1)
Number Elements: 2 (1,1)

Any help would much be appreciate. (Apologies for posting half
complete beforehand)

Best

Meldrum


I'm not sure if this algorithm will work for you. If not, you will need to get
into real equation parsing, and define more precisely what you mean by an
"element" and a "part"

But perhaps the number of "parts" would be equal to the number of operators,
and the number of "elements" would be equal to the number of operators + the
number of commas.

If we define an operator as anything in the set of [-+/*^=], then we do not
have to "add 1" as that would effectively be done by counting the initial "="
in the formula text.

You will need VBA to obtain the formula text, so we might as well do the whole
thing with a UDF:

=AnalyzeFormula(cell_ref, [elements])

The elements entry is optional; if false or blank, the formula will return the
number of "Parts" (3 in your example)

If True, it will return the number of "Elements" (5 in your example).

=================================================
Option Explicit

Function AnalyzeFormula(cell_ref As Range, Optional Elements As Boolean =
False) As Variant
Const sParts As String = "[-+/*^=]"
Const sElements As String = "[-+/*^=,]"
Dim sFormulaText As String
Dim sStr As String

Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
If Elements = True Then
re.Pattern = sElements
Else
re.Pattern = sParts
End If

If cell_ref.Count < 1 Then
AnalyzeFormula = CVErr(xlErrRef)
Exit Function
End If

sFormulaText = cell_ref.Formula
sStr = re.Replace(sFormulaText, "")

AnalyzeFormula = Len(sFormulaText) - Len(sStr)
End Function
=====================================
--ron


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Evaluating (Splitting out) a formula

On 31 Aug, 13:43, Ron Rosenfeld wrote:
On Sun, 31 Aug 2008 02:55:35 -0700 (PDT), meldrum_scotland





wrote:
Hi,


If there was a formula in cell A1 of the following: *=offset(C1,1,1) +
G1 * sheet2!A1


I would like to (using VBA) determine the number of elements in the
formula. *So for the above formula the evaluating would be:


Parts: 3 *(1=offset(C1,1,1), 2 = G1, 3=sheet2!A1)
Elements: 5 (C1, 1, 1, G1, sheet2!A1)


* *Ref Elements: 3 (C1, G1, sheet2!A1)
* *Number Elements: 2 (1,1)


Any help would much be appreciate. *(Apologies for posting half
complete beforehand)


Best


Meldrum


I'm not sure if this algorithm will work for you. *If not, you will need to get
into real equation parsing, and define more precisely what you mean by an
"element" and a "part"

But perhaps the number of "parts" would be equal to the number of operators,
and the number of "elements" would be equal to the number of operators + the
number of commas.

If we define an operator as anything in the set of [-+/*^=], then we do not
have to "add 1" as that would effectively be done by counting the initial "="
in the formula text.

You will need VBA to obtain the formula text, so we might as well do the whole
thing with a UDF:

=AnalyzeFormula(cell_ref, [elements])

The elements entry is optional; if false or blank, the formula will return the
number of "Parts" (3 in your example)

If True, it will return the number of "Elements" (5 in your example).

=================================================
Option Explicit

Function AnalyzeFormula(cell_ref As Range, Optional Elements As Boolean =
False) As Variant
Const sParts As String = "[-+/*^=]"
Const sElements As String = "[-+/*^=,]"
Dim sFormulaText As String
Dim sStr As String

Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
* * re.Global = True
If Elements = True Then
* * re.Pattern = sElements
Else
* * re.Pattern = sParts
End If

If cell_ref.Count < 1 Then
* * AnalyzeFormula = CVErr(xlErrRef)
* * Exit Function
End If

sFormulaText = cell_ref.Formula
sStr = re.Replace(sFormulaText, "")

AnalyzeFormula = Len(sFormulaText) - Len(sStr)
End Function
=====================================
--ron- Hide quoted text -

- Show quoted text -


Thank you very much for your replies.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Evaluating formula in VBA Walter Briscoe New Users to Excel 5 July 1st 09 10:33 AM
Evaluating more than 30 conditions using a formula bevchapman Excel Worksheet Functions 2 March 4th 09 06:37 PM
Evaluating (splitting out) a formula meldrum_scotland Excel Programming 0 August 31st 08 10:52 AM
evaluating text as if it were a formula Peter Facey Excel Discussion (Misc queries) 2 January 31st 08 05:20 PM
Formula not evaluating immediately Barb Reinhardt Excel Programming 7 August 18th 06 07:49 PM


All times are GMT +1. The time now is 10:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"