Solved

trailing spaces all columns

Posted on 2016-10-10
4
98 Views
Last Modified: 2016-10-11
I have a sheet with loads columns, and I have just noticed, in all the cells of data - all the cell have endless trailing spaces. I had been creating new columns aside of each of the original columns, and using =trim(A2) to get rid of them, but its taking a long time to complete. Can anyone think of a quick way to remove all trailing spaces on all columns in the report in one single go? Or is my current technique the only way possible.
0
Comment
Question by:pma111
4 Comments
 
LVL 46

Accepted Solution

by:
Martin Liss earned 250 total points
ID: 41837118
Here's a macro you can use.

Sub TrimAll()
Dim cel As Range
For Each cel In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
    cel = WorksheetFunction.Trim(cel)
Next
End Sub

Open in new window

1
 
LVL 27

Assisted Solution

by:Glenn Ray
Glenn Ray earned 125 total points
ID: 41837125
I use a third-party application to clean up ranges of data like that (ASAP Utilities).  But, here is a macro that will do the same.  You just highlight the range of data to clean up and run the macro.

Sub ReEnter()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim rng As Range
    Dim c As Object
    Set rng = Selection
    For Each c In rng
        c.Formula = Trim(c.Formula)
    Next c
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

Open in new window


-Glenn
0
 
LVL 30

Assisted Solution

by:Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj) earned 125 total points
ID: 41837155
Here is another one which will be faster to finish the task.
Sub RemoveTrailingSpaces()
Dim arr
Dim i As Long, j As Long
On Error Resume Next
arr = ActiveSheet.UsedRange.Value
For i = 1 To UBound(arr, 1)
   For j = 1 To UBound(arr, 2)
      arr(i, j) = Trim(arr(i, j))
   Next j
Next i
Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
End Sub

Open in new window

0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41838266
You could have carried on with the simple =TRIM(A2) method but go about it differently. Sounds like you are doing it one column at a time rather than doing it as a block.

Assuming your data is columns A to Z, in column AA put your =TRIM(A2) formula and copy across to column AZ. In other words just copy to the right until you have all columns of original data covered.

Select cells AA2 to AZ2 (or however far you needed to go right) and hover your cursor over the bottom right corner of AZ2 until it turns to a + sign. Double click and the formulas will be copied down until it reaches the last entry in column Z. You can then copy these new columns and rows and paste values over the original.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question