Excel
From Leo's Notes
This page was last edited on 15 April 2020, at 20:09.
Tasks
Trim Cell Spaces
If cells contain extraneous leading or trailing spaces and you wish to trim each cell's content:
- Select the range
- Hit
Alt+F11, then Insert -> Module. - Define a new module with the following code:
Sub TrimCellSpace() Dim Rng As Range Dim WorkRng As Range Set WorkRng = Application.InputBox("Range", "Set Range", Application.Selection.Address, Type:=8) For Each Rng In WorkRng Rng.Value = VBA.Trim(Rng.Value) Next End Sub
- Hit
F5and run against the selected range.