Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Remove leading spaces from excel worksheet

Hi,

Wonder if somone can help me please.

I have a spreadsheet with a number of cells that are blank to look at but
have a space in them as the first character.

Can someone tell me please how to write a macro that within a given range
looks at the cells that look blank and have the first character as a space
and then remove that space.

Many thanks

Chris
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Remove leading spaces from excel worksheet

sub RemoveApparentBlank()
Dim rng as Range, cell as Range
On error resume Next
set rng = Selection.SpecialCells(xlFormulas,xlTextValues)
On Error goto 0
if not rng is nothing then
for each cell in rng
if len(trim(cell)).Value = 0 then
cell.clearcontents
end if
Next
end if
set rng = Nothing
On error resume Next
set rng = Selection.SpecialCells(xlConstants,xlTextValues)
On Error goto 0
if not rng is nothing then
for each cell in rng
if len(trim(cell)).Value = 0 then
cell.clearcontents
end if
Next
end if
end sub

--
regards,
Tom Ogilvy

"ir26121973" wrote:

Hi,

Wonder if somone can help me please.

I have a spreadsheet with a number of cells that are blank to look at but
have a space in them as the first character.

Can someone tell me please how to write a macro that within a given range
looks at the cells that look blank and have the first character as a space
and then remove that space.

Many thanks

Chris

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Remove leading spaces from excel worksheet

Try this: just highlight the cell you want checked and run the macro

Sub RemoveSpaces()
Dim MyCell As Object
For Each MyCell In Selection
MyCell.Value = Trim(MyCell)
Next MyCell
End Sub

Sandy

ir26121973 wrote:
Hi,

Wonder if somone can help me please.

I have a spreadsheet with a number of cells that are blank to look at but
have a space in them as the first character.

Can someone tell me please how to write a macro that within a given range
looks at the cells that look blank and have the first character as a space
and then remove that space.

Many thanks

Chris


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Remove leading spaces from excel worksheet

Tom,

Thanks for this. I've put the code in VB but there seems to be an error in
this line.

if len(trim(cell)).Value = 0 then

VB highlights it in red and says it expects a Then or Goto.

Any advice please?

Thanks

Chris

"Tom Ogilvy" wrote:

sub RemoveApparentBlank()
Dim rng as Range, cell as Range
On error resume Next
set rng = Selection.SpecialCells(xlFormulas,xlTextValues)
On Error goto 0
if not rng is nothing then
for each cell in rng
if len(trim(cell)).Value = 0 then
cell.clearcontents
end if
Next
end if
set rng = Nothing
On error resume Next
set rng = Selection.SpecialCells(xlConstants,xlTextValues)
On Error goto 0
if not rng is nothing then
for each cell in rng
if len(trim(cell)).Value = 0 then
cell.clearcontents
end if
Next
end if
end sub

--
regards,
Tom Ogilvy

"ir26121973" wrote:

Hi,

Wonder if somone can help me please.

I have a spreadsheet with a number of cells that are blank to look at but
have a space in them as the first character.

Can someone tell me please how to write a macro that within a given range
looks at the cells that look blank and have the first character as a space
and then remove that space.

Many thanks

Chris

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Remove leading spaces from excel worksheet

Try this: just highlight the cell you want checked and run the macro

Sub RemoveSpaces()
Dim MyCell As Object
For Each MyCell In Selection
MyCell.Value = Trim(MyCell)
Next MyCell
End Sub

Sandy

ir26121973 wrote:
Hi,

Wonder if somone can help me please.

I have a spreadsheet with a number of cells that are blank to look at but
have a space in them as the first character.

Can someone tell me please how to write a macro that within a given range
looks at the cells that look blank and have the first character as a space
and then remove that space.

Many thanks

Chris




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Remove leading spaces from excel worksheet

Thanks for this Sandy.

Where would I put in a specific range as the spreadsheet does have quite a
lot of information in.

Thanks

Chris

"Sandy" wrote:

Try this: just highlight the cell you want checked and run the macro

Sub RemoveSpaces()
Dim MyCell As Object
For Each MyCell In Selection
MyCell.Value = Trim(MyCell)
Next MyCell
End Sub

Sandy

ir26121973 wrote:
Hi,

Wonder if somone can help me please.

I have a spreadsheet with a number of cells that are blank to look at but
have a space in them as the first character.

Can someone tell me please how to write a macro that within a given range
looks at the cells that look blank and have the first character as a space
and then remove that space.

Many thanks

Chris



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Remove leading spaces from excel worksheet

A small typo:

if len(trim(cell.Value)) = 0 then



ir26121973 wrote:

Tom,

Thanks for this. I've put the code in VB but there seems to be an error in
this line.

if len(trim(cell)).Value = 0 then

VB highlights it in red and says it expects a Then or Goto.

Any advice please?

Thanks

Chris

"Tom Ogilvy" wrote:

sub RemoveApparentBlank()
Dim rng as Range, cell as Range
On error resume Next
set rng = Selection.SpecialCells(xlFormulas,xlTextValues)
On Error goto 0
if not rng is nothing then
for each cell in rng
if len(trim(cell)).Value = 0 then
cell.clearcontents
end if
Next
end if
set rng = Nothing
On error resume Next
set rng = Selection.SpecialCells(xlConstants,xlTextValues)
On Error goto 0
if not rng is nothing then
for each cell in rng
if len(trim(cell)).Value = 0 then
cell.clearcontents
end if
Next
end if
end sub

--
regards,
Tom Ogilvy

"ir26121973" wrote:

Hi,

Wonder if somone can help me please.

I have a spreadsheet with a number of cells that are blank to look at but
have a space in them as the first character.

Can someone tell me please how to write a macro that within a given range
looks at the cells that look blank and have the first character as a space
and then remove that space.

Many thanks

Chris


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Remove leading spaces from excel worksheet

Replace "Selection" with your range ie Range("A1:D100")

ir26121973 wrote:
Thanks for this Sandy.

Where would I put in a specific range as the spreadsheet does have quite a
lot of information in.

Thanks

Chris

"Sandy" wrote:

Try this: just highlight the cell you want checked and run the macro

Sub RemoveSpaces()
Dim MyCell As Object
For Each MyCell In Selection
MyCell.Value = Trim(MyCell)
Next MyCell
End Sub

Sandy

ir26121973 wrote:
Hi,

Wonder if somone can help me please.

I have a spreadsheet with a number of cells that are blank to look at but
have a space in them as the first character.

Can someone tell me please how to write a macro that within a given range
looks at the cells that look blank and have the first character as a space
and then remove that space.

Many thanks

Chris




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Remove leading spaces from excel worksheet

Sandy thanks for this, works a treat.

Regards

Chris

"Sandy" wrote:

Replace "Selection" with your range ie Range("A1:D100")

ir26121973 wrote:
Thanks for this Sandy.

Where would I put in a specific range as the spreadsheet does have quite a
lot of information in.

Thanks

Chris

"Sandy" wrote:

Try this: just highlight the cell you want checked and run the macro

Sub RemoveSpaces()
Dim MyCell As Object
For Each MyCell In Selection
MyCell.Value = Trim(MyCell)
Next MyCell
End Sub

Sandy

ir26121973 wrote:
Hi,

Wonder if somone can help me please.

I have a spreadsheet with a number of cells that are blank to look at but
have a space in them as the first character.

Can someone tell me please how to write a macro that within a given range
looks at the cells that look blank and have the first character as a space
and then remove that space.

Many thanks

Chris




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
how do you remove leading spaces etc in cells? Anti-Spam New Users to Excel 11 August 29th 08 04:38 PM
remove leading spaces harwookf Excel Worksheet Functions 7 January 25th 08 01:56 PM
how do I remove leading spaces and leave the remianing spaces w Debi Excel Worksheet Functions 6 February 28th 07 03:29 PM
VBA help please - remove leading spaces Scott Wagner Excel Programming 6 February 24th 06 08:13 PM
Remove Leading Spaces Kirk P. Excel Discussion (Misc queries) 3 March 3rd 05 01:30 PM


All times are GMT +1. The time now is 06:13 PM.

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

About Us

"It's about Microsoft Excel"