Rather than let a good thread die how about :-
Function IncNum(str As String) As String
'--------------------------------
IncNum = ""
If Len(str) < 1 Then
Exit Function
End If
'--------------------------------
Dim Result As String
Dim re As New RegExp
Dim ms As MatchCollection
Dim m As Match
Dim Digits As String
Dim Incr As Long
Dim i As Long
'--------------------------------
Result = str
re.Pattern = "([0-9]+)"
re.Global = True
re.IgnoreCase = True
Set ms = re.Execute(Result)
Incr = 1
'--------------------------------
For i = ms.Count - 1 To 0 Step -1
Set m = ms(i)
Digits = m.Value
strFormat = String(m.Length, "0")
Digits = Format(Digits + Incr, strFormat)
If Len(Digits) > m.Length Then
Digits = Mid(Digits, 2)
Else
Incr = 0
End If
Mid(Result, m.FirstIndex + 1, m.Length) = Digits
Next 'm
'--------------------------------
IncNum = Result
If Incr > 0 Then
Err.Raise 5, Err.Source, "Unable to increment code string."
End If
'--------------------------------
End Function
Requires reference to ms regular expressions 1.0 or 5.5
Try
?IncNum("000asd999.999.999")
Anyone keeping count of the ways?
Regards John
qvb jat vke said:
"John Nurick" <
[email protected]>
??????
[email protected]...
On Thu, 1 Sep 2005 15:15:46 -0500, "Malcolm Cook"
The function I coded handles your sequence just fine. input "AH-1-G1"
output "AH-1-G2". n'est pas?
The one thing it doesn't seem to handle is leading zeros, e.g. input
"AB001", output "AB2" (unless you count the trivial case where the
string contains only digits).