PDA

View Full Version : Remove Special Characters From Text Or Remove Numbers From Text



Excel Fox
05-30-2013, 10:51 AM
Enum WhatToRemove
Text
Numeric
SpecialCharacters
End Enum
Function RemoveCharacters(ByVal StringToReplace As String, WhatRemove As WhatToRemove) As String
Dim RegEx As Object

Set RegEx = CreateObject("VBScript.RegExp")

With RegEx
.Global = True
Select Case WhatRemove
Case Text
.Pattern = "[A-Za-z]"
Case Numeric
.Pattern = "\d"
Case SpecialCharacters
.Pattern = "[\;+\.+\#+\!+\'+\*+\,+\+\^+\&+\*+\@+\(+\)]"
End Select
End With

RemoveCharacters= RegEx.Replace(StringToReplace, "")

Set RegEx = Nothing


End Function

Rick Rothstein
05-30-2013, 09:58 PM
Function RemoveUnnecessaryCharacters(ByVal StringToReplace As String, WhatRemove As WhatToRemove) As String

The part I highlighted in red is causing a "User-defined type not defined" error when I try to run your code?

Also, do you think you could have given that function an any longer name? :hypnotized:

Excel Fox
05-31-2013, 12:28 PM
Rick, my bad. Updated the code with the missing Enum Type. And the longer name, yeah, a tad long I must admit %D

snb
05-31-2013, 01:33 PM
@ExcelFox

Thanks for sharing.
I couldn't resist exploring; resulting in:


Enum gt_lost
Text = 0
Numeric = 1
Specialcharacters = 2
End Enum

Function RmChr(ByVal Str_chain As String, y As gt_lost) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = Array("[A-Za-z]", "\d", "[\~+\!+\@+\#+\$+\%+\^+\&+\*+\(+\)+\'+\-+\=+\{+\}+\[+\]+\:+\""+\++\;+\'+\<+\>+\?+\,+\.+\/]")(y)
RmChr = .Replace(Str_chain, "")
End With
End Function

Rick Rothstein
05-31-2013, 02:16 PM
@snb and ExcelFox...

And I couldn't help developting a non-RegExp function...

Enum WhatToRemove
Text = 0
numeric = 1
SpecialCharacters = 2
End Enum

Function RemoveCharacters(ByVal StringToReplace As String, WhatRemove As WhatToRemove) As String
Dim X As Long, Pattern() As String
Pattern = Split("[A-Za-z] # [#~!@$%^&*()'={}[:""+;<>?,./\-]")
If WhatRemove = SpecialCharacters Then StringToReplace = Replace(StringToReplace, "]", "")
For X = 1 To Len(StringToReplace)
If Mid(StringToReplace, X, 1) Like Pattern(WhatRemove) Then Mid(StringToReplace, X, 1) = Chr(1)
Next
RemoveCharacters = Replace(StringToReplace, Chr(1), "")
End Function


Note: I used snb's special characters list as it seemed more complete to me, although I also chose to remove the backslash character as well (you both allowed it to remain when special characters were to be removed).

snb
05-31-2013, 04:43 PM
Since languages are more complex than the A-Z alphabet:


Enum gt_lost
Text = 0
Numeric = 1
Specialcharacters = 2
ASCII_Text = 3
non_numeric = 4
End Enum

Function RmChr(ByVal Str_chain As String, y As gt_lost) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = Array("[A-Za-z]", "\d", "[\" & Replace(StrConv("~!@#$%^&*()'-={}[]:""+;'<>?,./\ (~!@#$%^&*()'-={}[]:&quot;&quot;+;'<>?,./\)|¦", 64), vbNullChar, "+\") & " ]", "\D+ ", "\D")(y)
RmChr = .Replace(Str_chain, "")
End With
End Function

Sub M_snb()
MsgBox RmChr("aa~d SDRT|éëïsdf 7\77ll""ll9 3 4,799 9@á¦#(*", 4)
End Sub