?
Solved

vba remove invisible special characters from string

Posted on 2014-03-18
6
Medium Priority
?
4,378 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
Independent Software Vendors: 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

770 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