LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Macro Help - Deleting two characters per line!

Do it in place - alter the cells themselves?

Sub Remove2LeftBlanks()
Dim cell as Range, rng as Range
Set rng = Range(cells(1,1),cells(rows.count).End(xlup))
for each cell in rng
if Left(cell.Value = " ") then
cell.Value = Mid(cell.Value,3,len(cell)-2)
end if
Next
End Sub

to put the results in the cell to the right:

Sub ReturnAllbut2LeftBlanks()
Dim cell as Range, rng as Range
Set rng = Range(cells(1,1),cells(rows.count).End(xlup))
for each cell in rng
if Left(cell.Value = " ") then
cell.Offset(0,1).Value = Mid(cell.Value,3,len(cell)-2)
end if
Next
End Sub

--
Regards,
Tom Ogilvy



"Ricky Pang" wrote in message
...
Hi Tom,
I have a similar question about spaces before the text.
=IF(ISBLANK(A1),"",IF(LEFT(A1,2)=" ",MID(A1,3,LEN(A1)),A1))

I'm using this formula to remove just the first 2 spaces only within the
text. How do I convert this to code? I've tried the Replace function
but it doesn't work right. When I enter just 2 spaces, it deletes all
spaces prior to the text. And I wanted to keep the remaining spaces
after the first 2 spaces.

I found this previously posted solution from Mr. Benson. I also like
your code which includes all of column A:

Sub RemoveSpaces()
Dim Rng As Range, Cell As Range
Set Rng = ActiveSheet.UsedRange
For Each Cell In Rng
If Not IsEmpty(Cell) _
And Len(Cell) = 1 _
And Right(Cell, 1) = Chr(32) _
And Not Cell.HasFormula Then _
Cell = RTrim(Cell)
Next
End Sub

How do you convert this to just remove the first 2 spaces from the left?
If the cell is empty, ignore and move down the list (all within column
A). Do I have to factor in the Chr(160) also?

Thanks so much,
Ricky

*** Sent via Developersdex http://www.developersdex.com ***



 
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 Characters Vinny0128 Excel Discussion (Misc queries) 2 May 7th 07 03:19 AM
Deleting characters that are not numbers jermsalerms Excel Discussion (Misc queries) 4 January 12th 06 08:06 PM
deleting characters PLPE[_12_] Excel Programming 4 July 7th 05 02:03 PM
how does excel store new line and line feed characters? ben h[_2_] Excel Programming 0 July 1st 04 02:34 AM
deleting last 4 characters from file name Hans Excel Programming 4 April 26th 04 01:50 PM


All times are GMT +1. The time now is 08:31 AM.

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"