Skip to content

Instantly share code, notes, and snippets.

@supergrass71
Created October 27, 2019 09:58
Show Gist options
  • Save supergrass71/0757549b329b488beac0a710ec913091 to your computer and use it in GitHub Desktop.
Save supergrass71/0757549b329b488beac0a710ec913091 to your computer and use it in GitHub Desktop.
Clean Trim #Word
Function CleanTrim(ByVal S As String, Optional ConvertNonBreakingSpace As Boolean = True) As String
'https://www.mrexcel.com/forum/excel-questions/923725-vba-remove-all-non-printable-special-characters-well-trim.html
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 = S
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment