VBA to trim after last space.

PCTech

Board Regular
Joined
Mar 24, 2005
Messages
210
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,
 

Some videos you may like

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,267
Office Version
  1. 365
Platform
  1. Windows
There is no Rept function in VBA, either us Application.Rept or the VBA function String.
 

Tetra201

MrExcel MVP
Joined
Oct 14, 2016
Messages
3,643
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
 

Phuoc

Active Member
Joined
Apr 29, 2016
Messages
313
Or try this:

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

Watch MrExcel Video

Forum statistics

Threads
1,127,328
Messages
5,624,036
Members
416,007
Latest member
csf

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
Top