Remove all Non-Printable characters as well as Clean and Trim; leave name text intact with spaces

blbat

Active Member
Joined
Mar 24, 2010
Messages
338
Office Version
  1. 2016
  2. 2013
I saw Rick Rothstein's code that removed Non-Printable Characters, and did a Clean and trim as well.

Can it be modified to leave spaces in the middle alone?
such as, if I have a column of names that may have leading or trailing spaces, or both...Like so:

Alfred E. Newman

John Paul Jones

I'd want the leading spaces removed, as well as the trailing spaces, but Leave the Names intact

Rick Rothstein's Code:

Code:
Function CleanTrim(ByVal S As String, Optional ConvertNonBreakingSpace As Boolean = True) As String
  Dim X As Long, CodesToClean As Variant
  CodesToClean = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
                       21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 129, 141, 143, 144, 157)
  If ConvertNonBreakingSpace Then S = Replace(S, Chr(160), " ")
  For X = LBound(CodesToClean) To UBound(CodesToClean)
    If InStr(S, Chr(CodesToClean(X))) Then S = Replace(S, Chr(CodesToClean(X)), "")
  Next
  CleanTrim = WorksheetFunction.Trim(S)
End Function
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I saw Rick Rothstein's code that removed Non-Printable Characters, and did a Clean and trim as well.

Can it be modified to leave spaces in the middle alone?
such as, if I have a column of names that may have leading or trailing spaces, or both...Like so:

Alfred E. Newman

John Paul Jones

I'd want the leading spaces removed, as well as the trailing spaces, but Leave the Names intact
Unless your names have multiple contiguous space within them, my code already does this. This line from my code...

CleanTrim = WorksheetFunction.Trim(S)

removes leading and trailing spaces and collapses multiple contiguous internal spaces down to single spaces. If you want to preserve multiple contiguous internal spaces while removing leading and trailing spaces, then do as HackSlash suggests and use Trim instead of WorksheetFunction.Trim...

CleanTrim = Trim(S)
 
Upvote 0

Forum statistics

Threads
1,215,632
Messages
6,125,909
Members
449,274
Latest member
mrcsbenson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top