Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 17
Default Removing leading and trailing space...

Hello,

I have a multi sheet excel spread sheet and I would like to remove the
leading and trailing space from all the Cells of these sheets. TRIM(A&i)
only does it on a single column how can this be done on all the columns and
sheets in a single run?

Any sample code/iead?

--
Jeff B Paarsa
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Removing leading and trailing space...

Option Explicit
Sub testme()

Dim myRng As Range
Dim myCell As Range
Dim wks As Worksheet

For Each wks In ActiveWorkbook.Worksheets
Set myRng = Nothing
On Error Resume Next
Set myRng = wks.Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
On Error GoTo 0

If myRng Is Nothing Then
MsgBox "nothing to fix--no constants on " & wks.Name & "."
Else
For Each myCell In myRng.Cells
'myCell.Value = Trim(myCell.Value)
'or
myCell.Value = Application.Trim(myCell.Value)
Next myCell
End If
Next wks

End Sub

There's a difference between the way VBA's Trim works and the way Excel's
=trim() works.

If your text we
....Jeffery....B.....Paarsa....
(spaces represented by dots)

Then VBA's trim will return:
Jeffery....B.....Paarsa

Application.trim() will return:
Jeffery.B.Paarsa

Application.trim() cleans up those multiple internal spaces.

Jeffery B Paarsa wrote:

Hello,

I have a multi sheet excel spread sheet and I would like to remove the
leading and trailing space from all the Cells of these sheets. TRIM(A&i)
only does it on a single column how can this be done on all the columns and
sheets in a single run?

Any sample code/iead?

--
Jeff B Paarsa


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 703
Default Removing leading and trailing space...

Hi Jeff

Try this:

Sub TrimAll()
Dim sh As Worksheet
Dim cell As Range
Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Sheets
For Each cell In sh.UsedRange
cell = Trim(cell.Value)
Next
Next
Application.ScreenUpdating = True
End Sub

Regards,
Per


On 8 Okt., 22:18, Jeffery B Paarsa wrote:
Hello,

I have a multi sheet excel spread sheet and I would like to remove the
leading and trailing space from all the Cells of these sheets. *TRIM(A&i)
only does it on a single column how can this be done on all the columns and
sheets in a single run?

Any sample code/iead?

--
Jeff B Paarsa


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting numbers with leading and trailing zero's Victoria Excel Worksheet Functions 7 June 17th 08 06:14 PM
Excluding only leading or trailing cells from a column PaladinWhite Excel Worksheet Functions 6 October 21st 07 06:48 PM
Removing leading/trailing spaces Chuda Excel Discussion (Misc queries) 2 September 12th 06 04:20 PM
How keep Leading/Trailing spaces when save in Excel? jorgejulio Excel Discussion (Misc queries) 0 August 1st 06 09:49 PM
Trailing Space / Format Question hagen Excel Discussion (Misc queries) 4 December 27th 04 11:01 PM


All times are GMT +1. The time now is 07:13 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"