Formula to extract only non-special characters: A to Z, a to z and 0 to 9

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hello all,

Like the title here suggests, what formula will extract characters A-Z , a-z and 0-9 from a cell.

Hence omit . spaces, dots, semicolon, question-mark, exclamation marks etc.

for instance.

From
A8^Kite?
abc 450
4xsection-
action?
54
12/31/2017

To
A8Kite
abc450
4xsection
action
54
12312017



Will appreciate any help.
Thanks.
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You can create a custom function like this.

Code:
Function FilterSpecial(s As String) As String
Dim Pattern As String: Pattern = "\W"
Dim strReplace As String: strReplace = ""
Dim regEx As New RegExp


With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .Pattern = Pattern
End With


If regEx.Test(s) Then
    FilterSpecial = regEx.Replace(s, strReplace)
End If


End Function

You will need to go into the VB Editor and go to Tools-->References, and add a reference to Microsoft VBScript Regular Expressions.
 
Upvote 0
never seen this before is it just a case of not using another line to say strReplace = ""

Dim strReplace As String: strReplace = ""
 
Last edited:
Upvote 0
Hi

if you are using Excel 2016 you could give this a try:
Code:
=TEXTJOIN("",1,REPT(MID(A1,ROW(INDIRECT("1:999")),1),IFERROR(--SEARCH(MID(A1,ROW(INDIRECT("1:999")),1),"0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z")>0,0)))

Haven't tested it though, since i dont have Excel 2016 on this PC...
 
Upvote 0
never seen this before is it just a case of not using another line to say strReplace = ""

Dim strReplace As String: strReplace = ""

Yeah, exactly. And the code could be written like this too I suppose..

Code:
Function FilterSpecial(s As String) As String
Dim Pattern As String: Pattern = "\W"
Dim regEx As New RegExp


With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .Pattern = Pattern
End With


If regEx.test(s) Then
    FilterSpecial = regEx.Replace(s, "")
End If


End Function
 
Upvote 0
Here is another macro that you can consider (it does not use Regular Expressions to do it "magic")...
Code:
Function AlphaNumeric(ByVal S As String) As String
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!A-Za-z0-9]" Then Mid(S, X) = " "
  Next
  AlphaNumeric = Replace(S, " ", "")
End Function
 
Upvote 0
@ lrobbo314
Your function doesn't seem to quite do what the OP has asked. For example,
- rows 6 & 8 below where the original text doesn't contain any characters that require removal (caused by your test in If regEx.Test(S) Then returning False)
- per row 9 below, your pattern allows through an underscore (because \W does not include the underscore character)

@ omairhe
Here is another regular expression function that I believe does what you want. My function also does not require you to do anything with Tools -> References in the vba window.

Code:
Function NonSpec(S As String) As String
  Static RX As Object
  
  If RX Is Nothing Then
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
  End If
  RX.Pattern = "[^a-zA-Z0-9]"
  NonSpec = RX.Replace(S, "")
End Function

Excel Workbook
ABC
1Peter_SSslrobbo314
2A8^Kite?A8KiteA8Kite
3abc 450abc450abc450
44xsection-4xsection4xsection
5action?actionaction
65454
712/31/20171231201712312017
8abcabc
9a?bc_76abc76abc_76
10
11abc%ghi()abcghiabcghi
Sheet1
 
Upvote 0
Right you are Peter. But the \W parameter does work as well. Minus the underscores. But it does fix the issue with the numbers. I just needed to take out the regEx.Test portion of the code.

Code:
Function FilterSpecial(s As String) As String
Dim Pattern As String: Pattern = "\W"
Dim regEx As New RegExp


With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .Pattern = Pattern
End With


FilterSpecial = regEx.Replace(s, "")


End Function
 
Last edited:
Upvote 0
Thanks much for your kind solutions. I am making a record list out of huge data so I needed exactly like this.

regards,

omair
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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