TRIM Formula

chef

Well-known Member
Joined
Jul 10, 2002
Messages
610
Office Version
  1. 365
  2. 2016
Hi,

My dept takes an electronic download of staff details( from Business Object) and into Excel in columns A to BG under various headings.
we have a few lookups but we need to manually apply the =trim formula in quite a few columns to get rid of trailing spaces in our download

if there a way of applying =trim to each of the columns automatically with a macro so we are left with no trailing spaces in any of the columns.

hope this makes sense and any help is greatly appreciated

regards
ian
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Code:
Sub trimall()
Dim rng As Range
Dim item As Range
Set rng = Range("A1", Range("BG56000").End(xlUp))
For Each item In rng
    item.Value = Trim(item.Value)
Next item
End Sub

Add this to a module and run when needed.
 
Upvote 0
Here's my solution using help range 10 columns right of the range to be trimmed. Change the help range location if needed.:
Code:
Sub TrimTheRange()
Dim Rng As Range
Application.ScreenUpdating = False
    Set Rng = ActiveSheet.UsedRange    'Set the range to trim here!
With Rng
        .Offset(0, .Columns.Count + 10).Formula = "=trim(A1)" 'Trims the Rng values using help range 10 columns to the rigth of rng
        .Value = .Offset(0, .Columns.Count + 10).Value 'Replaces the original values with trimmed values
        .Offset(0, .Columns.Count + 10).Clear 'Clears the help range
End With
End Sub
 
Upvote 0
Hi,

many thanks for all the help....will save a lot of time

regards
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,741
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top