Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro Help - Deleting two characters per line!


Basically, I have a report which i download from an online website each
day.

The report is automatically formatted in Excel, however, in each column
the first two characters are spaces before it goes onto the number.

I basically want a macro that goes through each row on column A and
deletes these two blank spaces.

Can this be achieved?

If so, how?

Cheers

Brian


--
mccrimmon


------------------------------------------------------------------------
mccrimmon's Profile: http://www.excelforum.com/member.php...fo&userid=6338
View this thread: http://www.excelforum.com/showthread...hreadid=437466

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Macro Help - Deleting two characters per line!

For Each myCell In Range("A1",Range("A65536").End(xlUp)
myCell.Value = Mid(myCell.Value,3,Len(myCell.Value))
Next myCell

HTH,
Bernie
MS Excel MVP


"mccrimmon" wrote in message
...

Basically, I have a report which i download from an online website each
day.

The report is automatically formatted in Excel, however, in each column
the first two characters are spaces before it goes onto the number.

I basically want a macro that goes through each row on column A and
deletes these two blank spaces.

Can this be achieved?

If so, how?

Cheers

Brian


--
mccrimmon


------------------------------------------------------------------------
mccrimmon's Profile: http://www.excelforum.com/member.php...fo&userid=6338
View this thread: http://www.excelforum.com/showthread...hreadid=437466



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Macro Help - Deleting two characters per line!

Dim myCell as Range
For Each myCell In Range("A1",Cells(row.count,1).End(xlUp))
myCell.Value = Ltrim(myCell.Value)
Next myCell

would be another way. to trim both leading and trailing spaces, replace
Ltrim with Trim

--
Regards,
Tom Ogilvy



"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
For Each myCell In Range("A1",Range("A65536").End(xlUp)
myCell.Value = Mid(myCell.Value,3,Len(myCell.Value))
Next myCell

HTH,
Bernie
MS Excel MVP


"mccrimmon" wrote

in message
...

Basically, I have a report which i download from an online website each
day.

The report is automatically formatted in Excel, however, in each column
the first two characters are spaces before it goes onto the number.

I basically want a macro that goes through each row on column A and
deletes these two blank spaces.

Can this be achieved?

If so, how?

Cheers

Brian


--
mccrimmon


------------------------------------------------------------------------
mccrimmon's Profile:

http://www.excelforum.com/member.php...fo&userid=6338
View this thread:

http://www.excelforum.com/showthread...hreadid=437466





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Macro Help - Deleting two characters per line!

Tom,

Down-loaded files often contain char 160 (or other non-printing character) as a space, which TRIM
doesn't deal with properly, which is why I went to the MID solution.

Another solution could be:

Range("A:A").Replace What:=Chr(160) & Chr(160), Replacement:="", LookAt:=xlPart

with the 160 replaced with the proper code, unless there were interior double spaces that were
important.

HTH,
Bernie
MS Excel MVP


"Tom Ogilvy" wrote in message ...
Dim myCell as Range
For Each myCell In Range("A1",Cells(row.count,1).End(xlUp))
myCell.Value = Ltrim(myCell.Value)
Next myCell

would be another way. to trim both leading and trailing spaces, replace
Ltrim with Trim

--
Regards,
Tom Ogilvy



"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
For Each myCell In Range("A1",Range("A65536").End(xlUp)
myCell.Value = Mid(myCell.Value,3,Len(myCell.Value))
Next myCell

HTH,
Bernie
MS Excel MVP


"mccrimmon" wrote

in message
...

Basically, I have a report which i download from an online website each
day.

The report is automatically formatted in Excel, however, in each column
the first two characters are spaces before it goes onto the number.

I basically want a macro that goes through each row on column A and
deletes these two blank spaces.

Can this be achieved?

If so, how?

Cheers

Brian


--
mccrimmon


------------------------------------------------------------------------
mccrimmon's Profile:

http://www.excelforum.com/member.php...fo&userid=6338
View this thread:

http://www.excelforum.com/showthread...hreadid=437466







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default Macro Help - Deleting two characters per line!

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 ***


  #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 ***



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default Macro Help - Deleting two characters per line!

Hi Tom,
Thank-you for coming to my rescue yet again. When I ran both codes, I
got this same error message: Compile Error: Argument not optional.
While debugging, the [left] is highlighted (which is the 5th line down
of the Remove2LeftBlanks code).

My apologies, I'm unsure what you meant by Do it in place.

I hope to see your reply again.

Much appreciated.

Ricky


*** Sent via Developersdex http://www.developersdex.com ***
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro Help - Deleting two characters per line!


Mac,

Try this:

Sub RemovePreSpaces()
Dim rng As Range, c As Range

Set rng = ActiveSheet.UsedRange
For Each c In rng
c.Value = Mid(c, 3)
Next

End Su

--
david
-----------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...fo&userid=2064
View this thread: http://www.excelforum.com/showthread.php?threadid=43746

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Macro Help - Deleting two characters per line!

You don't really need a macro for this... There is find and replace which you
can run against the column replacing all instances of double blank with
nothing. If that won't work for you there is a trim() or ltrim() function
right in Excel that you can use... If you need it to be a macro then you can
try this

Sub RemoveBlank()
dim rngCurrent as range
dim rngToSearch as range
dim wks as worksheet

set wks = activesheet
set rngToSearch = intersect(wks.Range("A:A"), wks.usedrange)
for each rngCurrent in rngToSearch
rngCurrent.value = ltrim(rngcurrent.value)
next wksCurrent

end sub

--
HTH...

Jim Thomlinson


"mccrimmon" wrote:


Basically, I have a report which i download from an online website each
day.

The report is automatically formatted in Excel, however, in each column
the first two characters are spaces before it goes onto the number.

I basically want a macro that goes through each row on column A and
deletes these two blank spaces.

Can this be achieved?

If so, how?

Cheers

Brian


--
mccrimmon


------------------------------------------------------------------------
mccrimmon's Profile: http://www.excelforum.com/member.php...fo&userid=6338
View this thread: http://www.excelforum.com/showthread...hreadid=437466


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Macro Help - Deleting two characters per line!

ltrim is a vba function--it's not built into excel.

And edit|replace could cause havoc depending on what else is in those cells.

Jim Thomlinson wrote:

You don't really need a macro for this... There is find and replace which you
can run against the column replacing all instances of double blank with
nothing. If that won't work for you there is a trim() or ltrim() function
right in Excel that you can use... If you need it to be a macro then you can
try this

Sub RemoveBlank()
dim rngCurrent as range
dim rngToSearch as range
dim wks as worksheet

set wks = activesheet
set rngToSearch = intersect(wks.Range("A:A"), wks.usedrange)
for each rngCurrent in rngToSearch
rngCurrent.value = ltrim(rngcurrent.value)
next wksCurrent

end sub

--
HTH...

Jim Thomlinson

"mccrimmon" wrote:


Basically, I have a report which i download from an online website each
day.

The report is automatically formatted in Excel, however, in each column
the first two characters are spaces before it goes onto the number.

I basically want a macro that goes through each row on column A and
deletes these two blank spaces.

Can this be achieved?

If so, how?

Cheers

Brian


--
mccrimmon


------------------------------------------------------------------------
mccrimmon's Profile: http://www.excelforum.com/member.php...fo&userid=6338
View this thread: http://www.excelforum.com/showthread...hreadid=437466



--

Dave Peterson


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 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 12:33 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"