Solved

trailing spaces all columns

Posted on 2016-10-10
4
58 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 45

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 28

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 31

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
This article will shed light on the latest trends when it comes to your resume building needs. For far too long, the traditional CV format has monopolized the recruitment market.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now