Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Delete leading or trailing blank spaces in cell - an example

That's a good point Tom. I've made the corrections below.
Cheers,


Sub DeleteTrailingBlankSpacesOnTheRight()
'Deletes trailing blank spaces on the right of a string
Dim cell As Range
Dim rng As Range
On Error Resume Next

Set rng = Selection.SpecialCells(xlCellTypeConstants, xlTextValues)
For Each cell In Intersect(Selection, rng)

cell.Value = RTrim(cell.Value)

Next cell
End Sub

Sub DeleteLeadingBlankSpacesOnTheLeft()
'Deletes leading blank spaces on the left of a string
Dim cell As Range
Dim rng As Range
On Error Resume Next
Set rng = Selection.SpecialCells(xlCellTypeConstants, xlTextValues)
For Each cell In Intersect(Selection, rng)

cell.Value = LTrim(cell.Value)

Next cell
End Sub

Sub DeleteBlankSpacesOnTheLeftAndRight()
'Deletes blank spaces to the left and right of a string
Dim cell As Range
On Error Resume Next
Dim rng As Range
Set rng = Selection.SpecialCells(xlCellTypeConstants, xlTextValues)
For Each cell In Intersect(Selection, rng)
cell.Value = Trim(cell.Value)
Next cell
End Sub

Function RemoveTrailingBlankSpacesOnTheRight(TargetCell As Range) As String
'Removes trailing blank spaces on the right of a string
On Error Resume Next
RemoveTrailingBlankSpacesOnTheRight = RTrim(TargetCell.Value)
End Function

Function RemoveLeadingBlankSpacesOnTheLeft(TargetCell As Range) As String
'Removes leading blank spaces on the left of a string
On Error Resume Next
RemoveLeadingBlankSpacesOnTheLeft = LTrim(TargetCell.Value)
End Function


Function RemoveBlankSpacesOnTheLeftAndRight(TargetCell As Range) As String
'Removes blank spaces to the left and right of a string

On Error Resume Next
RemoveBlankSpacesOnTheLeftAndRight = Trim(TargetCell.Value)

End Function
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
Deleting leading spaces in a cell Dunc Fowler Excel Discussion (Misc queries) 4 May 4th 07 08:19 AM
how do I remove leading spaces and leave the remianing spaces w Debi Excel Worksheet Functions 6 February 28th 07 03:29 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
Strip leading spaces from cell Pete Excel Worksheet Functions 3 July 31st 05 02:26 AM


All times are GMT +1. The time now is 09:53 PM.

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

About Us

"It's about Microsoft Excel"