How to delete everything but "normal" characters

hellothere4

New Member
Joined
May 9, 2011
Messages
17
Hello, I'm just looking for a simple way to delete all characters that aren't of the standard type, like a-z, 0-9, .'!? THANKS!!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
See Help for the CLEAN() function?
 
Upvote 0
Code:
Function ClearString(ByVal sWord As String) As String

    Dim i As Integer
    Dim sNewWord As String, sCharacter As String, sPattern As String
    Dim re As RegExp, m As Match
    
    sWord = "BCDa%"
    sPattern = "[a-z0-9.'!?]"

    Set re = New RegExp
    With re
        .IgnoreCase = True
        .Pattern = sPattern
    End With
    
    For i = 1 To Len(sWord)
        sCharacter = Mid$(sWord, i, 1)
        If re.Test(sCharacter) Then
            sNewWord = sNewWord & sCharacter
        End If
    Next
    
    ClearString = sNewWord
    
End Sub
 
Upvote 0
This code requires library. Go here: Tools -> Reference -> Microsoft VBScript Regular Expressions 5.5.
 
Upvote 0
Hi

If Clean() does not solve your problem then I think a udf approach like Sektor suggests will.

I think that in this case you don't need to both loop through the characters and use Regex's.

2 options.

1 - Use a RegEx, for ex.:

Code:
Function ClearString1(ByVal sWord As String) As String
 
    With CreateObject("VBScript.RegExp")
        .Pattern = "[^a-zA-Z0-9.'!?]"
        ClearString1 = .Replace(sWord, "")
    End With
End Function

2 - loop through the characters:

Code:
Function ClearString2(ByVal sWord As String) As String
    Dim i As Integer
    Dim sNewWord As String, sCharacter As String, sPattern As String
    
    sPattern = "[a-zA-Z0-9.'!?]"
    For i = 1 To Len(sWord)
        sCharacter = Mid$(sWord, i, 1)
        If sCharacter Like sPattern Then
            sNewWord = sNewWord & sCharacter
        End If
    Next
    
    ClearString2 = sNewWord
    
End Function
 
Upvote 0
I've used a different approach to the regex to remove all the non-confomring caharacters in one shot

If you add this VBA function to your workbook then you can then call it either as a
a) worksheet function
for example
=RegexRemove(A1)
in cell B1 to remove any non a-z0-9!.' from A1
b) via other code

hth

Dave

Code:
Function RegexRemove(strIn As String)
    Dim Regex
    Set Regex = CreateObject("vbscript.regexp")
    With Regex
        .IgnoreCase = True
        .Global = True
        .Pattern = "[^\w+\s\.\!']"
        RegexRemove = .Replace(strIn, vbNullString)
    End With
End Function
 
Upvote 0
dang .. same as pgc01's code which appeared in the time for me to complete my post except that I added a space to the ignore list (/s) and used case ignore and /w in place of a-zA-Z0-9

Cheers

Dave
 
Upvote 0
Hi

In my previous post I forgot to set the Global property of the RegEx:

Code:
Function ClearString1(ByVal sWord As String) As String
 
    With CreateObject("VBScript.RegExp")
        .Pattern = "[^a-zA-Z0-9.'!?]"
[COLOR=black]        .Global = True
[/COLOR]        ClearString1 = .Replace(sWord, "")
    End With
End Function
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,771
Members
452,941
Latest member
Greayliams

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top