Rtrim not working

VBABeginner1

New Member
Joined
Dec 6, 2014
Messages
31
Code:
Sub Concate()


Dim i As Long
Dim c As String






    Dim WS1 As Worksheet
    Dim WS2 As Worksheet
    
    Set WS1 = Worksheets("BEFORE AND AFTER")
    Set WS2 = Worksheets("COMPARE")
    
For i = 4 To 10004
WS2.Cells(i, 123) = WS1.Cells(i, 188) & " " & WS1.Cells(i, 189)
c = RTrim(WS2.Cells(i, 123))
Next i

Whenever I run this, it still has the trailing space to the right. I am doing this to correct the problem if the 2nd cell in the concatenate is blank....... (WS1.cells(i, 189) in this example.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Are you sure the character is actually a space?
 
Upvote 0
maybe try TRIM, otherwise you might have a hidden space
 
Upvote 0
Code:
Sub Concate()


Dim i As Long
Dim c As String






    Dim WS1 As Worksheet
    Dim WS2 As Worksheet
    
    Set WS1 = Worksheets("BEFORE AND AFTER")
    Set WS2 = Worksheets("COMPARE")
    
For i = 4 To 10004
WS2.Cells(i, 123) = WS1.Cells(i, 188) & " " & WS1.Cells(i, 189)
[B][COLOR="#FF0000"]c = RTrim(WS2.Cells(i, 123))[/COLOR][/B]
Next i

Whenever I run this, it still has the trailing space to the right. I am doing this to correct the problem if the 2nd cell in the concatenate is blank....... (WS1.cells(i, 189) in this example.
To follow up on what Norie posted, I thing you may have non-breaking spaces (ASCII 160) rather than normal spaces (ASCII 32) in your cells. This can happen if you copy text from webpages. See if changing the red highlighted line of code to this makes it work correctly...

c = RTrim(Replace(WS2.Cells(i, 123), Chr(160), " "))
 
Last edited:
Upvote 0
I tried this : c = RTrim(Replace(WS2.Cells(i, 123), Chr(160), " "))
and tried it as Trim (without the R)

I still have the same issue.

Would there be a way to alter this code that if the second part of the concatenate is blank that it just ignores it?
 
Upvote 0
If you want to write the trimmed value to the cell try
Code:
For i = 4 To 10004
   ws2.Cells(i, 123) = Ws1.Cells(i, 188) & " " & Ws1.Cells(i, 189)
   ws2.Cells(i, 123).Value = RTrim(ws2.Cells(i, 123).Value)
Next i
 
Last edited:
Upvote 0
If you want to write the trimmed value to the cell try
Code:
For i = 4 To 10004
   ws2.Cells(i, 123) = Ws1.Cells(i, 188) & " " & Ws1.Cells(i, 189)
   ws2.Cells(i, 123).Value = RTrim(ws2.Cells(i, 123).Value)
Next i


That did it!!

Thank you so much!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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