Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vba remove invisible special characters from string

Posted on 2014-03-18
6
Medium Priority
?
4,640 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 23

Accepted Solution

by:
Ejgil Hedegaard earned 1200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Assisted Solution

by:COACHMAN99
COACHMAN99 earned 400 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 46

Assisted Solution

by:aikimark
aikimark earned 400 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

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!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

604 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