Excel

From Leo's Notes
Revision as of 23:26, 27 April 2020 by Leo (talk | contribs)
This page was last edited on 27 April 2020, at 23:26.

Tasks

Trim Cell Spaces

If cells contain extraneous leading or trailing spaces and you wish to trim each cell's content:

  1. Select the cell range you wish to trim
  2. Hit Alt + F11. In the VB window, insert a new module: Insert -> Module.
  3. Define a new module with the following code:
    Sub TrimCellSpace()
            On Error Resume Next
    	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
    
  4. Hit F5 and run against the selected range.