VBA to trim after last space.

PCTech

Board Regular
Joined
Mar 24, 2005
Messages
215
I have a formula that works
=TRIM(RIGHT(SUBSTITUTE(A3," ",REPT(" ",LEN(A3))),LEN(A3)))

and a macro that works. But not together. Can someone tell me where I need to add quote marks or what it will take to make this work?

VBA Code:
Sub SplitLastSpace()
Application.ScreenUpdating = False
Dim c As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each c In Range("A2:A" & Lastrow)
         c.Value = c.Value = Trim(Right(Substitute(A2, " ", Rept(" ", Len(A2))), Len(A2)))
    Next
Application.ScreenUpdating = True
End Sub

I get Compile error: Sub or Function not definded with Rept highlighted.

Thanks,
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
There is no Rept function in VBA, either us Application.Rept or the VBA function String.
 
Upvote 0
Try
VBA Code:
Sub SplitLastSpace()
Application.ScreenUpdating = False
Dim c As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each c In Range("A2:A" & Lastrow)
         c.Value = Split(c.Value, " ")(UBound(Split(c.Value, " ")))
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Or try this:

VBA Code:
c.Value = Mid(c.Value, InStrRev(Trim(c.Value), " ") + 1)
 
Upvote 0

Forum statistics

Threads
1,213,553
Messages
6,114,279
Members
448,562
Latest member
Flashbond

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