Old school iterating over all characters in a string:
Function IdentifyCharacterSequences(s As String) As String Dim i As Long Dim charCounter As Long Dim currentCharType As String Dim sOut As String sOut = "" charCounter = 1 currentCharType = CharType(Mid(s, 1, 1)) For i = 2 To Len(s) If (Not CharType(Mid(s, i, 1)) = currentCharType) Or (i = Len(s)) Then sOut = sOut & charCounter & currentCharType currentCharType = CharType(Mid(s, i, 1)) charCounter = 1 Else charCounter = charCounter + 1 End If Next i IdentifyCharacterSequences = sOut End Function
The following helper function is used. Please note that non-alphanumeric characters are identified by the letter "X". You can easily change this to suit your needs.
Function CharType(s As String) As String If s Like "[Az]" Then CharType = "A" ElseIf s Like "[0-9]" Then CharType = "N" Else CharType = "X" 'Or raise an error if non-alphanumerical chars are unacceptable. End If End Function
Usage example:

source share