View Single Post
  #16   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default adding numbers with letters

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