Remove only one space, if there is a space, from the end of text.

pglufkin

Board Regular
Joined
Jun 19, 2005
Messages
127
Was wondering if someone could help me.

Was thinking something along the lines of the procedure below, but I only want to remove one space at the end of my text within a cell, if there is a space. Any quick ideas? Thx.

Code:
Sub hth() 
    Dim c As  Range 
     
    For Each c In Range("H1", Range("H" & Rows.Count).End(xlUp)) 
        c.Value = Trim(c.Value) 
    Next c 
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi,
Here's THe Solution:
Code:
Sub hth()
    Dim c As Range
     
    For Each c In Range("H1", Range("H" & ROWS.Count).End(xlUp))
        i = c.Value
        i = Left(i, Len(i) - InStrRev(i, " ")) & Right(i, Len(i) - (InStrRev(i, " ") - 1))
    Next c
End Sub

ZAX
 
Upvote 0
Thx, however your procedure does not write anything to c.value, doesn't change my cell with the needed change.
 
Upvote 0
Your procedure produces a blank where there was once text . . . i had to add a couple of lines. Can anyone else help me?
 
Upvote 0
Maybe

Code:
Sub hth()
    Dim c As Range
     
    For Each c In Range("H1", Range("H" & Rows.Count).End(xlUp))
        If Right(c.Value, 1) = " " Then
            c.Value = StrReverse(Mid(StrReverse(c.Value), 2))
        End If
    Next c
End Sub

M.
 
Upvote 0
Bingo, thank you :)
I think this non-looping macro will also do what you wanted...
Code:
Sub RemoveOneSpaceFromEndOnly()
  Dim Addr As String
  Addr = "H1:H" & Cells(Rows.Count, "H").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF(RIGHT(@)="" "",LEFT(@,LEN(@)-1),@)", "@", Addr))
End Sub
 
Last edited:
Upvote 0
Rick,

Just curious. What is the role of @ in your formula. New for me.
It "represents" the value of each cell in the range?

M.
 
Upvote 0
Rick,

Just curious. What is the role of @ in your formula. New for me.
It "represents" the value of each cell in the range?
It is simply a "stand-in" character for the address (you can use any character that will not be included in the argument's text) that gets replaced all at once by the Replace function... it avoids having to concatenate the address into the argument's text string over and over again which makes constructing/reading the argument a little easier.
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,769
Members
448,991
Latest member
Hanakoro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top