Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default macro to remove spaces at the end of a cell

ok, here is a tough one i think. I have a file that i copy into excel every
day. while all the information is the same, in the B column there is a random
amount of spaces included by the program that gives me the data. Thanks to
Dave i now have a macro that I can use to compare two sheets for duplicates,
problem is that its not finding the duplicates due to the different number of
spaces.. anyone know how to make a macro that removes all the spaces? the
values in the B columns are all different lengths.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default macro to remove spaces at the end of a cell


cLastRow = Cells(Rows.Count,"B").End(xlUp).Row
For i = 1 To cLastRow
Cells(i,"B").Value = Trim(Cells(i,"B").Value
Next i

--
HTH

Bob Phillips

"Michael A" wrote in message
...
ok, here is a tough one i think. I have a file that i copy into excel

every
day. while all the information is the same, in the B column there is a

random
amount of spaces included by the program that gives me the data. Thanks to
Dave i now have a macro that I can use to compare two sheets for

duplicates,
problem is that its not finding the duplicates due to the different number

of
spaces.. anyone know how to make a macro that removes all the spaces? the
values in the B columns are all different lengths.



  #3   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default macro to remove spaces at the end of a cell

you can use the function TRIM like this:

=TRIM(B1)

or the folllowing macros:

this if need to remove spaces at the beginning and the end as well as
duplicate ones in the middle of the string:

Sub test()
Dim LastRow As Long, i As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
For i = 1 To LastRow
.Cells(i, 2) = WorksheetFunction.Trim(.Cells(i, 2))
Next i
End With
End Sub

or this if all spaces need to be removed:

Sub test2()
Dim rng As Range
With ActiveSheet
Set rng = Intersect(.UsedRange, .Columns("B"))
If Not rng Is Nothing Then _
rng.Replace " ", ""
End With
End Sub

Regards,
KL

"Michael A" wrote in message
...
ok, here is a tough one i think. I have a file that i copy into excel
every
day. while all the information is the same, in the B column there is a
random
amount of spaces included by the program that gives me the data. Thanks to
Dave i now have a macro that I can use to compare two sheets for
duplicates,
problem is that its not finding the duplicates due to the different number
of
spaces.. anyone know how to make a macro that removes all the spaces? the
values in the B columns are all different lengths.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default macro to remove spaces at the end of a cell

Hi
Something like [B1] = trim([A1])
will remove blank spaces from either end of a text string.

Regards

Andrew Bourke



Michael A wrote:
ok, here is a tough one i think. I have a file that i copy into excel every
day. while all the information is the same, in the B column there is a random
amount of spaces included by the program that gives me the data. Thanks to
Dave i now have a macro that I can use to compare two sheets for duplicates,
problem is that its not finding the duplicates due to the different number of
spaces.. anyone know how to make a macro that removes all the spaces? the
values in the B columns are all different lengths.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default macro to remove spaces at the end of a cell

thank you both very much.. I know realize its columns A B and C .. so i could
do this couldln't I?

Sub test2()
Dim rng As Range
With ActiveSheet
Set rng = Intersect(.UsedRange, .Columns("A3:C"))
If Not rng Is Nothing Then _
rng.Replace " ", ""
End With
End Sub


"KL" wrote:

you can use the function TRIM like this:

=TRIM(B1)

or the folllowing macros:

this if need to remove spaces at the beginning and the end as well as
duplicate ones in the middle of the string:

Sub test()
Dim LastRow As Long, i As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
For i = 1 To LastRow
.Cells(i, 2) = WorksheetFunction.Trim(.Cells(i, 2))
Next i
End With
End Sub

or this if all spaces need to be removed:

Sub test2()
Dim rng As Range
With ActiveSheet
Set rng = Intersect(.UsedRange, .Columns("B"))
If Not rng Is Nothing Then _
rng.Replace " ", ""
End With
End Sub

Regards,
KL

"Michael A" wrote in message
...
ok, here is a tough one i think. I have a file that i copy into excel
every
day. while all the information is the same, in the B column there is a
random
amount of spaces included by the program that gives me the data. Thanks to
Dave i now have a macro that I can use to compare two sheets for
duplicates,
problem is that its not finding the duplicates due to the different number
of
spaces.. anyone know how to make a macro that removes all the spaces? the
values in the B columns are all different lengths.






  #6   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default macro to remove spaces at the end of a cell

..Columns("A:C")

not

..Columns("A3:C")

KL

"Michael A" wrote in message
...
thank you both very much.. I know realize its columns A B and C .. so i
could
do this couldln't I?

Sub test2()
Dim rng As Range
With ActiveSheet
Set rng = Intersect(.UsedRange, .Columns("A3:C"))
If Not rng Is Nothing Then _
rng.Replace " ", ""
End With
End Sub


"KL" wrote:

you can use the function TRIM like this:

=TRIM(B1)

or the folllowing macros:

this if need to remove spaces at the beginning and the end as well as
duplicate ones in the middle of the string:

Sub test()
Dim LastRow As Long, i As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
For i = 1 To LastRow
.Cells(i, 2) = WorksheetFunction.Trim(.Cells(i, 2))
Next i
End With
End Sub

or this if all spaces need to be removed:

Sub test2()
Dim rng As Range
With ActiveSheet
Set rng = Intersect(.UsedRange, .Columns("B"))
If Not rng Is Nothing Then _
rng.Replace " ", ""
End With
End Sub

Regards,
KL

"Michael A" wrote in message
...
ok, here is a tough one i think. I have a file that i copy into excel
every
day. while all the information is the same, in the B column there is a
random
amount of spaces included by the program that gives me the data. Thanks
to
Dave i now have a macro that I can use to compare two sheets for
duplicates,
problem is that its not finding the duplicates due to the different
number
of
spaces.. anyone know how to make a macro that removes all the spaces?
the
values in the B columns are all different lengths.






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 I remove leading spaces and leave the remianing spaces w Debi Excel Worksheet Functions 6 February 28th 07 03:29 PM
How to remove spaces from a word in a cell Jerry Excel Discussion (Misc queries) 5 January 15th 07 08:11 PM
Remove all spaces in a cell... killertofu Excel Worksheet Functions 8 February 20th 06 08:17 PM
how to remove spaces in a cell/column P.M.SANJAY Excel Discussion (Misc queries) 4 January 7th 06 08:21 AM
how to remove spaces in a middle of a cell example '25 566 589. P.M.SANJAY Excel Discussion (Misc queries) 1 January 6th 06 07:23 PM


All times are GMT +1. The time now is 03:20 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"