View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
DataFreakFromUtah DataFreakFromUtah is offline
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