Solved

trailing spaces all columns

Posted on 2016-10-10
4
81 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 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 32

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

863 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