E
EagleOne
Yes you are correct. I did specify "following" Operators. As we all know, asking the correct
question leads to a best answer. I was not clear and/or not complete.
That said, "Houston - we have a solution!"
Thanks again Ron for your skill, knowledge and empathy for we NewBees.
EagleOne
question leads to a best answer. I was not clear and/or not complete.
That said, "Houston - we have a solution!"
Thanks again Ron for your skill, knowledge and empathy for we NewBees.
EagleOne
Ron Rosenfeld said:Great! The error went away. All is fine unless the first Ref is negative.
i.e., -123456789+ .......
It seems that the first "sign" is not considered.
In short:
FormulaStr = "123456789+'Summary 2-22-2007'!H8+'Summary 3-22-2007'!H22-A1+(144/6)*'Summary
4-22-2007'!H23-B1+9876"
is processed the same as
FormulaStr = "-123456789+'Summary 2-22-2007'!H8+'Summary 3-22-2007'!H22-A1+(144/6)*'Summary
4-22-2007'!H23-B1+9876"
Note the "-" at the beginning of the entire string.
In your specifications, you indicated that you wanted the operator "after" the
string, so I did not take into account an initial "-".
If you want an initial "-" to be treated differently, then perhaps the
following. I have added an optional "-" to the various string definitions (and
also had to change the operator submatch item from 4 to 5).
===============================================
Option Explicit
Sub Parse()
Dim Parsed() As String
Dim objRegExp As Object
Dim objMatchCollection As Object
Dim sPattern As String
Dim i As Long
Dim FormulaStr As String
FormulaStr = Replace(ActiveCell.Formula, Chr(10), "")
sPattern = _
"(-?(('[\s\S]*?'!\w+)|(\([\s\S]*?\))|([^-+*/^<>=]+)))([-+*/^<>][<>=]?|$)"
Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.MultiLine = True
objRegExp.Pattern = sPattern
If objRegExp.Test(FormulaStr) = True Then
Set objMatchCollection = objRegExp.Execute(FormulaStr)
ReDim Parsed(1 To objMatchCollection.Count, 1 To 2)
For i = 1 To objMatchCollection.Count
Parsed(i, 1) = objMatchCollection(i - 1).submatches(0)
Parsed(i, 2) = objMatchCollection(i - 1).submatches(5)
Next i
End If
For i = 1 To UBound(Parsed)
Debug.Print "Parsed(" & i&; ") = " & Parsed(i, 1), Parsed(i, 2)
Next i
End Sub
==================================================
The following formula:
=-123456789+'Summary 2-22-2007'!H8+'Summary 3-22-2007
'!H22-A1+(-144/6)*'Summary 4-22-2007'!H23-B1+9876
Gives this as a result:
Parsed(1) = -123456789 +
Parsed(2) = 'Summary 2-22-2007'!H8 +
Parsed(3) = 'Summary 3-22-2007 '!H22 -
Parsed(4) = A1 +
Parsed(5) = (-144/6) *
Parsed(6) = 'Summary 4-22-2007'!H23 -
Parsed(7) = B1 +
Parsed(8) = 9876
Note that I also stuck a leading "-" after the parenthesis term, and it seemed
to parse correctly.
--ron