Jason Campbell
Board Regular
- Joined
- Mar 22, 2016
- Messages
- 77
Hi All,
I have come across this cracking macro to quickly iron out extra spaces inputted erroneously by users! The thing is it removes the additional spaces on the Left, Middle & to the Right of the text entry.
Can anyone help me interpret these specific parts of macro, bits highlighted in red & purple, shown directly below?
Selection.Cells.SpecialCells(xlCellTypeConstants, 23).Select --> What dose the (xlCellTypeConstants, 23) part of syntax mean?
How do the spaces and speech marks come into play below, when trimming the left, middle and right of the text?
(Trim(MyCell.Value), " ", " ")
(Trim(MyCell.Value), " ", " ")
(Trim(MyCell.Value), " ", " ")
(Trim(MyCell.Value), " ", " ")
Any explanation to help me understand what is being specified here would gratefully be received.
Thanks in advance.
---------------------------------------------------------------------------------------------------------------------------------------------
Full working macro syntax below:
I have come across this cracking macro to quickly iron out extra spaces inputted erroneously by users! The thing is it removes the additional spaces on the Left, Middle & to the Right of the text entry.
Can anyone help me interpret these specific parts of macro, bits highlighted in red & purple, shown directly below?
Selection.Cells.SpecialCells(xlCellTypeConstants, 23).Select --> What dose the (xlCellTypeConstants, 23) part of syntax mean?
How do the spaces and speech marks come into play below, when trimming the left, middle and right of the text?
(Trim(MyCell.Value), " ", " ")
(Trim(MyCell.Value), " ", " ")
(Trim(MyCell.Value), " ", " ")
(Trim(MyCell.Value), " ", " ")
Any explanation to help me understand what is being specified here would gratefully be received.
Thanks in advance.
---------------------------------------------------------------------------------------------------------------------------------------------
Full working macro syntax below:
Code:
Sub TrimText()
' This module will trim extra spaces from BOTH SIDES and excessive spaces from inside the text.
Dim MyCell As Range
On Error Resume Next
Selection.Cells.SpecialCells(xlCellTypeConstants, 23).Select
For Each MyCell In Selection.Cells
MyCell.Value = Application.WorksheetFunction.Substitute(Trim(MyCell.Value), " ", " ")
MyCell.Value = Application.WorksheetFunction.Substitute(Trim(MyCell.Value), " ", " ")
MyCell.Value = Application.WorksheetFunction.Substitute(Trim(MyCell.Value), " ", " ")
MyCell.Value = Application.WorksheetFunction.Substitute(Trim(MyCell.Value), " ", " ")
Next
On Error GoTo 0
End Sub
Last edited by a moderator: