View Single Post
  #17   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default adding numbers with letters

On Wed, 9 Jan 2008 17:58:20 -0500, "Rick Rothstein \(MVP - VB\)"
wrote:

Here is a modification to the code I just posted which should survive most (if not all) combinations of characters following a valid number in each of the addends making up your text. That is, a text string like this should be evaluated fine....

88 1/2../ grn-5.5 /. / wht+88 1/2//.//grn

I'm not sure if your text could ever be this malformed, but the code will function correctly it if it can be. Use the same instructions for implementing the code below my signature as I gave you in my previous posting.


Wow, that's pretty malformed.


Yeah, I know<g... but I found the text the OP asked us to evaluate to be malformed to begin with, so we are only talking of degree.

Here's a bit shorter routine that should do the same, though, even with this
degree of malformation:

=================================
Option Explicit
Function SumNums(str As String) As Double
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "([A-Za-z])|(\D[./]\D)"
SumNums = Evaluate(re.Replace(str, ""))
End Function
===================================


I tried to modify your Pattern string (see my latest post to you in this sub-thread), but that is not the direction I was heading in when I gave up.<g There is no question that in certain circumstance, such as this OP's request, Regular Expressions truly rule! Nice going!!!

Rick


Thank you.

A problem with the malformations is that we don't really know how to interpret
it. I took the point of view that the "/" needed to be part of a fraction, so
I could test to make sure it was surrounded by digits. The same is true of the
".". But maybe a standalone "/" should be interpreted as a "divide" operator.
What about other standalone possible operators?

I also make the assumption that a "-" is a negation or subtraction, even if it
stands alone. But for consistency with your routine, I did not make that
assumption with the "/"

Anyway, it's an interesting exercise.

--ron