Trim using Macro

MASOODAHMAD

Board Regular
Joined
Mar 28, 2012
Messages
105
Platform
  1. MacOS
Hi,

I would like to remove any trailing spaces from a number of columns on sheet 1. Please suggest a good macro.

Thanks,

Masood
 
Hi,

Something like this? Note that I've declared my column numbers in the array, if you don't know your column numbers straight when code is started let me know and we can adapt it.

Code:
Sub TrimMyDefinedColumns()

        Dim arrColumns()
        Dim lngColumn As Long, lngLastRow As Long, lngRow As Long
        
        With Application
                .ScreenUpdating = False
                .EnableEvents = False
                .Calculation = xlCalculationManual
        End With
                
        arrColumns = Array(1, 3, 5, 7, 9, 14)
        
        With Sheet1
                For lngColumn = 0 To UBound(arrColumns)
                        With .Columns(arrColumns(lngColumn))
                                lngLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
                                For lngRow = 1 To lngLastRow
                                        .Cells(lngRow, 1).Value = Trim(.Cells(lngRow, 1).Value)
                                Next lngRow
                        End With
                Next lngColumn
        End With

        With Application
                .ScreenUpdating = True
                .EnableEvents = True
                .Calculation = xlCalculationAutomatic
        End With

End Sub

Also worth noting that Lyn's reference is to a very good tool and it may not hurt to be a little more grateful to all responses on here and not always assume you need x when y maybe an even better solution for you :)
 
Upvote 0
This replaces all multiple spaces by the single one,
as well as provides the left & right space chars trimming:
Rich (BB code):

Sub MyTrim()
  With ActiveSheet.UsedRange
    .Value = Application.Trim(.Value)
  End With
End Sub
 
Upvote 0
This replaces all multiple spaces by the single one,
as well as provides the left & right space chars trimming:
Rich (BB code):
Sub MyTrim()
With ActiveSheet.UsedRange
  .Value = Application.Trim(.Value)
End With
End Sub
Very interesting. For that to work across all cells in the range, you must specifically specify Application as the object from which Trim is descended. If you use WorksheetFunction for the object (or even Application.WorksheetFunction) you get a Type Mismatch error. So what does that mean... there are two identically named Excel Trim functions that work differently from each other? Very interesting indeed. I wonder if there are any other "worksheet functions" that work differently when descended from the Application object as opposed to WorksheetFunction object? More importantly, I wonder if there is any documentation describing this different functionality?
 
Upvote 0
Hi Rick,

I’m not familiar with the other artefact functions like this.

AFAIK all application members have the same functionality as WorksheetFunction members.

We can’t find Trim function/method of Application in the Object Browser's window even among the hidden members.
I'm guessing that Application.Trim was reserved for Excel automation purpose.

It was not supported in Excel 95 and seems came from Excel 97
Hopefully it accepts 2D-array argument.
It is not documented, not formally exists but it works. :)

Regards,
Vlad
 
Last edited:
Upvote 0
Hi,

I tried the below code, but got an error:


Sub MyTrim()
With ActiveSheet.UsedRange
.Value = Application.Trim(.Value)
End With
End Sub</pre>
Please advise something short and better.

My data comes in 4 to 5 columns with trailing spaces. I want to get rid of them, retaining the original data. I do it manually with trim command and then pasting it over as values. I just want to get rid of that manual work.

Please advise.

@Lynn, sorry if it hurts. downloading is not allowed in my office, therefore, I just asked the vba code for the problem.

@Mike Blackman, I haven't tried your code, I will test it tomorrow. The code looks very lengthy.

Thanks,

Masood Ahmad
 
Upvote 0

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