Home » excel » excel – Trimming spaces

excel – Trimming spaces

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to parse left and right empty spaces, nbsp, \n, \t, etc. from all Excel cells in a certain range.

I’m using the following macro:

Sub TRIM_CELLS()

    'Clean all conditional formating
    Cells.FormatConditions.Delete

    'improve performance
    Application.ScreenUpdating = False

    Dim all_cells_range As String
    all_cells_range = "A1:A10"

    'Trim all those cells
    Range(all_cells_range).Select
    For Each cell In Selection.Cells
        cell.Value = Application.Substitute(Application.Substitute(CStr(cell.Value), vbLf, vbCr), vbCr, "")
        cell = WorksheetFunction.Trim((Application.Clean(Replace(cell.Value, Chr(160), " "))))
    Next cell
End Sub

Something like “Maria Tavares ” doesn’t get trimmed properly.

How to&Answers:

@Nick: I tried to use your idea and I think the problem is the char itself… Assuming the following loop works as expected I would get the char that is causing the problem.

Take a look at this image:

"space is not a space"

But nothing gets printed in that place.

Sub TRIM_CELLS()

    'Clean all conditional formating
    Cells.FormatConditions.Delete

    'improve performance
    Application.ScreenUpdating = False

    Dim all_cells_range As String
    all_cells_range = "A1:A2"

    'Trim all those cells
    Range(all_cells_range).Select
    For Each cell In Selection.Cells
        For I = 1 To 255
            cell = WorksheetFunction.Substitute(cell, Chr(I), I)
        Next I

    Next cell
End Sub

Answer:

I use a function that removes any special characters that you define.

Function RemoveSpecialCharacters(wks As Worksheet, strRange As String, var As Variant)
Dim rngAddress As Range, cell As Range, I&

'e.g strRange - "E2:E"

With wks
Set rngAddress = .Range(strRange & .Cells(Rows.count, "A").End(xlUp).row)

  For I = LBound(var) To UBound(var)
    For Each cell In rngAddress
            cell = WorksheetFunction.Substitute(cell, var(I), " ")
                Else
            cell = WorksheetFunction.Substitute(cell, var(I), "")
    Next cell
  Next I
End With

End Function

You could call the function like this:

RemoveSpecialCharacters worksheetname, "A1:A", Array(Chr(9), Chr(10), Chr(13), Chr(39))

Where Chr(10) is linefeed character, chr(9) is the tab character etc.

See this link for what other Chr codes stand for.

UPDATE:

Try this to remove the weird “spy” character from the cell.

RemoveSpecialCharacters worksheetname, "A1:A", Array(Chr(160))

Answer:

Ended up doing a replacement with that “space”… not a great solution but fixed my problem… Just would like to know what char it was…

Sub TRIM_CELLS()

'Clean all conditional formating
Cells.FormatConditions.Delete

'improve performance
Application.ScreenUpdating = False

Dim all_cells_range As String
all_cells_range = "A1:A2"
Range(all_cells_range).Select
For Each cell In Selection.Cells
    cell.Replace What:=" ", Replacement:=" ", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    cell.Value = Application.Substitute(Application.Substitute(CStr(cell.Value), vbLf, vbCr), vbCr, "")
    cell.Value = WorksheetFunction.Trim(cell)
Next cell

end sub