SHARE
TWEET

Incrementing Text String

AntidotE Nov 30th, 2011 33 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function IncrementTextString(txt As String) As String
  2.     'taken from http://www.freevbcode.com/ShowCode.asp?ID=2071
  3.    'enhanced to take in numbers also
  4.    Dim L As Integer, i As Integer, c As Integer
  5.     Dim S As String
  6.     S = txt
  7.     L = Len(S)
  8.    
  9.     For i = L To 1 Step -1    'go thru the string, right to left
  10.        c = Asc(Mid(S, i, 1)) 'ASCII code of the i-th character
  11.        Select Case c
  12.             Case 65 To 89, 97 To 121, 48 To 56  'A-Y or a-y or 0-8
  13.                S = Left(S, i - 1) & Chr(c + 1) & Mid(S, i + 1)
  14.                 Exit For
  15.             Case 90    'Z
  16.                S = Left(S, i - 1) & "A" & Mid(S, i + 1)
  17.             Case 122   'z
  18.                S = Left(S, i - 1) & "a" & Mid(S, i + 1)
  19.             Case 57    '0
  20.                S = Left(S, i - 1) & "0" & Mid(S, i + 1)
  21.         End Select
  22.         'in the last two cases, we need to continue the loop:
  23.    Next i
  24.     If i = 0 Then
  25.         IncrementTextString = String(L + 1, 65) 'grow the string
  26.    Else
  27.         IncrementTextString = S
  28.     End If
  29. End Function
  30.  
  31.  
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top