Solved

vba remove invisible special characters from string

Posted on 2014-03-18
6
3,825 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now