Excel VBA - take out leading and trailing non-alpha characters in a given txt string

Posted on 2011-10-26
Last Modified: 2012-06-21
I have a cell (actually single-field merged range) with a text string.  I need to take out the initial and trailing end characters that are NOT alpha.  

So if the string is
;&3abc@wer!;er3ruu.rst.!     the result will be     3abc@wer!;er3ru.rst
only the initial and trailing non-alpha chars were removed.  The embedded non-alpha chars remain.

The solution could be something like two loops, one for the initial chars, the other for the end chars:

while initial char is not alpha
  remove initial char

while last char is not alpha
  remove last char

Thanks, --Andres
Question by:AndresHernando
    LVL 92

    Assisted Solution

    by:Patrick Matthews
    Add this function to a regular VBA module:

    Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
        Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True, _
        Optional MultiLine As Boolean = False)
        ' Function written by Patrick G. Matthews.  You may use and distribute this code freely,
        ' as long as you properly credit and attribute authorship and the URL of where you
        ' found the code
        ' For more info, please see:
        ' This function relies on the VBScript version of Regular Expressions, and thus some of
        ' the functionality available in Perl and/or .Net may not be available.  The full extent
        ' of what functionality will be available on any given computer is based on which version
        ' of the VBScript runtime is installed on that computer
        ' This function uses Regular Expressions to parse a string, and replace parts of the string
        ' matching the specified pattern with another string.  The optional argument ReplaceAll
        ' controls whether all instances of the matched string are replaced (True) or just the first
        ' instance (False)
        ' If you need to replace the Nth match, or a range of matches, then use RegExpReplaceRange
        ' instead
        ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
        ' set it to True
        ' If you use this function from Excel, you may substitute range references for all the arguments
        ' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
        ' where a large number of calls to this function are made, making RegX a static variable that
        ' preserves its state in between calls significantly improves performance
        Static RegX As Object
        If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
        With RegX
            .Pattern = PatternStr
            .Global = ReplaceAll
            .IgnoreCase = Not MatchCase
            .MultiLine = MultiLine
        End With
        RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    End Function

    Open in new window

    Then, use it in a formula like this:


    For more about Regular Expressions, please see:
    LVL 10

    Expert Comment

    Use the clean function
    LVL 92

    Expert Comment

    by:Patrick Matthews
    The CLEAN() function removes non-printing characters (e.g., tab, line feed, carriage return), not non-alphanumeric characters.
    LVL 27

    Accepted Solution

    This code follows your original idea of having two loops process the leading three and trailing three characters.   It acts only on the active cell (i.e., where cursor is placed)

    The current example only displays the result in a message box; the final line (22) that is commented out would overwrite the existing value.  Just remove the apostrophe (and comment out the preceding line)

    Sub Clean_Leading_Trailing()
        Dim c, x As Integer
        Dim strCurrent, strCleaned As String
        strCurrent = ActiveCell.Value
        strCleaned = Mid(strCurrent, 4, Len(strCurrent) - 6)
        For x = 3 To 1 Step -1
            c = Asc(Mid(strCurrent, x, 1))
            Select Case c
                Case 48 To 57, 65 To 90, 97 To 122
                    strCleaned = Mid(strCurrent, x, 1) & strCleaned
            End Select
        Next x
        For x = 2 To 0 Step -1
            c = Asc(Mid(strCurrent, Len(strCurrent) - x, 1))
            Select Case c
                Case 48 To 57, 65 To 90, 97 To 122
                    strCleaned = strCleaned & Mid(strCurrent, Len(strCurrent) - x, 1)
            End Select
        Next x
        MsgBox "Original: " & strCurrent & vbLf & "Revised: " & strCleaned
        'ActiveCell.value = strCleaned
    End Sub

    Open in new window


    Author Comment

    Glenn, nicely done.  A couple tweaks and I'm making it work for all cases (not just initial/trailing three characters).  Thanks!  I'm awarding you 400pts

    Matthew, your solution adds value.  Not quite for my current problem, but it will come in handy down the road.  Thanks.  Remaining 100pts to you.

    etech0, thanks for your input too.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
    Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
    The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now