Multiplying a Number Found in an Alphanumeric String
On Fri, 7 Dec 2012 16:28:59 +0000, Randomerz wrote:
The one constant throughout is that the "$" sign precedes every price.
Perhaps the following will do what you require. It multiplies every value that both looks like a number and is also preceded by a "$", by mult, and replaces it in the original string.
=====================================
Option Explicit
Function MultDollars(s As String, mult As Double) As String
Dim re As Object, mc As Object
Dim i As Long
Set re = CreateObject("vbscript.regexp")
With re
.Global = True
.Pattern = "\$(\d+(?:\.\d+)?)"
End With
If re.test(s) = True Then
Set mc = re.Execute(s)
For i = mc.Count - 1 To 0 Step -1
s = Left(s, mc(i).firstindex + 1) & _
Format(mc(i) * mult, "#0.00") & _
Mid(s, mc(i).firstindex + 1 + mc(i).Length)
Next i
MultDollars = s
End If
End Function
=============================
|