2

How can I split up AB2468123 with

I tried something along these lines:

myStr = "AB2468123"
split(myStr, "1" OR "2" OR "3"......."9")

I want to get only alphabet (letters) only.

Thanks.

Community
  • 1
  • 1
K.W.
  • 141
  • 1
  • 15

4 Answers4

5

How about this to retrieve only letters from an input string:

Function GetLettersOnly(str As String) As String
    Dim i As Long, letters As String, letter As String

    letters = vbNullString

    For i = 1 To Len(str)
        letter = VBA.Mid$(str, i, 1)

        If Asc(LCase(letter)) >= 97 And Asc(LCase(letter)) <= 122 Then
            letters = letters + letter
        End If
    Next
    GetLettersOnly = letters
End Function

Sub Test()
    Debug.Print GetLettersOnly("abc123")      // prints "abc"
    Debug.Print GetLettersOnly("ABC123")      // prints "ABC"
    Debug.Print GetLettersOnly("123")         // prints nothing
    Debug.Print GetLettersOnly("abc123def")   // prints "abcdef"
End Sub

Edit: for completeness (and Chris Neilsen) here is the Regex way:

Function GetLettersOnly(str As String) As String
    Dim result As String, objRegEx As Object, match As Object

    Set objRegEx = CreateObject("vbscript.regexp")

    objRegEx.Pattern = "[a-zA-Z]+"
    objRegEx.Global = True
    objRegEx.IgnoreCase = True

    If objRegEx.test(str) Then
        Set match = objRegEx.Execute(str)
        GetLettersOnly = match(0)
    End If
End Function

Sub test()
    Debug.Print GetLettersOnly("abc123") //prints "abc"
End Sub
Alex P
  • 12,249
  • 5
  • 51
  • 70
  • 1
    If relevant note that will return only the first match (making this line redundant `objRegEx.Global = True`) - will work for `abc123` but not `abc123d` – brettdj Jul 12 '14 at 08:38
4

This is what i have found out that works the best. It may be somewhat basic, but it does the job :)

    Function Split_String(Optional test As String = "ABC111111") As Variant
    For i = 1 To Len(test)
    letter = Mid(test, i, 1)
        If IsNumeric(letter) = True Then
           justletters = Left(test, i - 1)
           justnumbers = Right(test, Len(test) - (i - 1))
           Exit For
        End If
    Next
   'MsgBox (justnumbers)
   'MsgBox (justletters)

   'just comment away the answer you want to have :)
   'Split_String = justnumbers
   'Split_String = justletters

   End Function
Philip Meholm
  • 391
  • 1
  • 4
  • I think you mixed up your `justnumbers` and `justletters`. – guitarthrower Jul 11 '14 at 15:36
  • 1
    This works when the input string is letters followed by numbers e.g. `abc123`. However it does not work for `1abc123` or `abc123abc`. So long as OP always has letters followed by numbers then this is fine. – Alex P Jul 11 '14 at 18:20
4

Simpler single shot RegExp

Sub TestIt()
MsgBox CleanStr("AB2468123")
End Sub

Function CleanStr(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Pattern = "[^a-zA-Z]+"
    .Global = True
    CleanStr = .Replace(strIn, vbNullString)
End With
End Function
brettdj
  • 54,857
  • 16
  • 114
  • 177
0

Possibly the fastest way is to parse a Byte String:

Function alpha(txt As String) As String
  Dim b, bytes() As Byte: bytes = txt
  For Each b In bytes
    If Chr(b) Like "[A-Za-z]" Then alpha = alpha & Chr(b)
  Next b
End Function
  • More information here.
ashleedawg
  • 20,365
  • 9
  • 72
  • 105