Solved

vba remove invisible special characters from string

Posted on 2014-03-18
6
4,055 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 22

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

735 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