PC Review


Reply
Thread Tools Rate Thread

Delete leading or trailing blank spaces in cell - an example

 
 
DataFreakFromUtah
Guest
Posts: n/a
 
      15th Sep 2004
No question here, just a couple of procedures for the archive.

Sub DeleteTrailingBlankSpacesOnTheRight()
'Deletes trailing blank spaces on the right of a string
Dim cell As Range
For Each cell In Selection
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
For Each cell In Selection
cell.Value = LTrim(cell.Value)
Next cell
End Sub



Search criteria: delete leading spaces on left
delete spaces on left
delete spaces to the left
delete trailing spaces on right
delete spaces on left
delete spaces to the left
delete blank spaces on left of string
delete blank spaces on right of string
delete blank spaces to left of string
delete blank spaces to right of string
 
Reply With Quote
 
 
 
 
Tom Ogilvy
Guest
Posts: n/a
 
      15th Sep 2004
Note that this will destroy any formulas in the selection and errors on a
cell containing an error value.

--
Regards,
Tom Ogilvy




"DataFreakFromUtah" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> No question here, just a couple of procedures for the archive.
>
> Sub DeleteTrailingBlankSpacesOnTheRight()
> 'Deletes trailing blank spaces on the right of a string
> Dim cell As Range
> For Each cell In Selection
> 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
> For Each cell In Selection
> cell.Value = LTrim(cell.Value)
> Next cell
> End Sub
>
>
>
> Search criteria: delete leading spaces on left
> delete spaces on left
> delete spaces to the left
> delete trailing spaces on right
> delete spaces on left
> delete spaces to the left
> delete blank spaces on left of string
> delete blank spaces on right of string
> delete blank spaces to left of string
> delete blank spaces to right of string



 
Reply With Quote
 
DataFreakFromUtah
Guest
Posts: n/a
 
      16th Sep 2004
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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I have to worry about Access storing Trailing or leading spaces Steve Stad Microsoft Access Form Coding 5 10th Jan 2010 12:31 AM
Highlighting leading or trailing spaces JackGombola Microsoft Excel Programming 2 28th Aug 2009 02:08 PM
removing leading and trailing spaces =?Utf-8?B?UmFt?= Microsoft Excel Programming 1 31st Oct 2007 10:57 PM
Removing leading/trailing spaces =?Utf-8?B?Q2h1ZGE=?= Microsoft Excel Misc 2 12th Sep 2006 04:20 PM
How keep Leading/Trailing spaces when save in Excel? jorgejulio Microsoft Excel Misc 0 1st Aug 2006 09:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:42 PM.