Solved

vba remove invisible special characters from string

Posted on 2014-03-18
6
3,937 Views
Last Modified: 2014-03-19
vba excel routine:

Is there anyway to revise this routine to remove "Invisible" characters from a string that is being passed?

much like asap utilities ?


cD is my variable string

cD = UserForm2.TextBox37

' strip special characters first NEW way
    sSpecialChars = "!@#$%^&*()_+={}|[]:;'<>?,.~`"
    For i = 1 To Len(sSpecialChars)
        cD = Replace$(cD, Mid$(sSpecialChars, i, 1), " ")
    Next
    cD = cD
' THIS EXCEL TRIM FUNCTION
cD = Application.Trim(cD)
cD = cD

Open in new window



Thanks
fordraiders
0
Comment
Question by:fordraiders
6 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39937713
an alternative is to build the new string with only the ones you want (rather than deleting the ones you don't)
i.e. if the char isn't in your sSpecialChars  then add to cd
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39937736
You've got it all pretty much, how about this:
Function sFnRemoveSC(sInput As String) As String
Dim lLoop As Long, sSpecialChars As String

sSpecialChars = "!@#$%^&*()_+={}|[]:;'<>?,.~`"
    
For lLoop = 1 To Len(sSpecialChars)
    sInput = Replace$(sInput, Mid$(sSpecialChars, lLoop, 1), " ")
Next
    
sFnRemoveSC = sInput


End Function

Open in new window


You can call it as

CD=sFnRemoveSC(CD)

or straight from your worksheet

=sFnRemoveSC(B5)
0
 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 300 total points
ID: 39938020
If you by "Invisible" characters mean "not on the keyboard", then use the code number and add to the string using the character code.

sSpecialChars = "!@#$%^&*()_+={}|[]:;'<>?,.~`" + Chr(9) + Chr(10)

If you in above code from nutsch, replace space " " with an empty string "" the character is removed.
sInput = Replace$(sInput, Mid$(sSpecialChars, lLoop, 1), "")
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 7

Assisted Solution

by:COACHMAN99
COACHMAN99 earned 100 total points
ID: 39938374
cD = UserForm2.TextBox37
Dim tmpStr As String, i As Long
' strip special characters first NEW way
    sSpecialChars = "abcdefghijklmnopqrstuvwxyz1234567890"
    For i = 1 To Len(cD)
        If InStr(1, LCase(sSpecialChars(Mid(cD, i, 1)))) > 0 Then
          tmpStr = tmpStr & Mid(cD, i, 1)
        End If
    Next
    cD = tmpStr
' THIS EXCEL TRIM FUNCTION
cD = Application.Trim(cD)
cD = cD
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 100 total points
ID: 39939176
This is a function that uses a regular expression object to remove the unwanted characters.
Option Explicit


Public Function CleanString(parmString) As String
    Static oRE As Object
    Const cUnwantedChars As String = "[!@#$%^&*()_+={}|\[\]:;'<>\?,.~`]"
    If oRE Is Nothing Then
        Set oRE = CreateObject("vbscript.regexp")
        oRE.Global = True
        oRE.Pattern = cUnwantedChars
    End If
    If oRE.test(parmString) Then
        CleanString = oRE.Replace(parmString, vbNullString)
    Else
        CleanString = parmString
    End If
End Function

Open in new window

If you know the characters that you will allow, then then you can use something like this pattern.  Here, my pattern is the negation of the letters and numbers and space -- anything else matches the pattern.
Option Explicit


Public Function CleanString2(parmString) As String
    Static oRE As Object
    Const cNotAllowedChars As String = "[^A-Za-z0-9 ]"
    If oRE Is Nothing Then
        Set oRE = CreateObject("vbscript.regexp")
        oRE.Global = True
        oRE.Pattern = cNotAllowedChars
    End If
    If oRE.test(parmString) Then
        CleanString2 = oRE.Replace(parmString, vbNullString)
    Else
        CleanString2 = parmString
    End If
End Function

Open in new window

Of course, you can parameterize the pattern string to make this function more flexible.
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39941487
hgholt
If you by "Invisible" characters mean "not on the keyboard", then use the code number and add to the string using the character code.

sSpecialChars = "!@#$%^&*()_+={}|[]:;'<>?,.~`" + Chr(9) + Chr(10)

This was simple and helped the best
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

777 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