Applying the Excel's Trim Function on the Entire Workbook.

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hey Excel Gurus,

I have a large data with multiple worksheets. Please my requirement this time is for a vba code that performs trimming on the existing data present in all worksheets of my workbook.

For example if text is (without the quotation marks):

" abc def "

the code runs and check for data cells on each sheet and voila...

"abc def"

Thank you and much appreciation.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Here you are.

Code:
Sub trimmer()
      Dim Current As Worksheet

' Loop through all of the worksheets in the active workbook.
For Each Current In Worksheets
For Each cell In Current.UsedRange.Cells
    cell.Value = Trim(cell.Value)
Next cell
Next Current
End Sub
 
Upvote 0
Try this
Code:
Sub Trim_All_Sheets()
    Dim ws As Worksheet, cel As Range
        With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
                For Each ws In ThisWorkbook.Sheets
                    For Each cel In ws.Cells.SpecialCells(xlCellTypeConstants)
                        cel.Value = WorksheetFunction.Trim(cel.Value)
                    Next cel
                Next ws
            .Calculation = xlCalculationAutomatic
    End With
End Sub
 
Upvote 0
Just spotted reply from @jmacleary
- be aware of the difference between the 2 solutions, both of which are equally valid but do different things
- choose the one that does what you want
@Yongle solution replaces ONLY those cells with constant values (ie it ignores formulas)
@jmacleary solution replaces each cell value with trimmed value and removes formulas from all cells
 
Last edited:
Upvote 0
Well done Yongle. It was careless of me to overlook the effect on formulas. I also like that you turn off screen updating and calculation to speed it up.

Your solution just needs .ScreenUpdating=True adding after the line: .Calculation = xlCalculationAutomatic
 
Last edited:
Upvote 0
I am getting a type mismatch error for both the codes and the values do not trim.
 
Upvote 0
@jmacleary
You do not need to turn ScreenUpdating back on at the end of the code, as it will happen automatically.
@omairhe
Which lines give the error?
 
Upvote 0
@jmacleary
You do not need to turn ScreenUpdating back on at the end of the code, as it will happen automatically.
@omairhe
Which lines give the error?


Runtime error 13 on the following lines
Code:
    cell.Value = Trim(cell.Value)

and

Code:
                        cel.Value = WorksheetFunction.Trim(cel.Value)

should I test it on a new blank workbook?
 
Upvote 0
Do any of your cells contain error values such as #N/A, #NAME? etc
 
Last edited:
Upvote 0
Do any of your cells contain error values such as #N/A, #NA ME? etc

Yes I have #N/A as text values in my original workbook.tested it with and without the n/a on a blank workbook and the problem seems to be gone.

Can the code be altered to make it bypass this situation. Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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