Formula to remove all characters after the last space in a string.

saundej

New Member
Joined
Mar 22, 2012
Messages
3
Hi,

I'm trying to find a formula which will remove all characters to the right of the last space in a string. I've found:

=LEFT(A1,FIND("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1)

which seems to do the trick, however I'd like to add some extra intelligence to it so that it only applies if the string contains 25 characters or more.

Does anybody have any ideas?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi

Perhaps a simple modification like:

=IF(LEN(A1)>=25,=LEFT(A1,FIND("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1),A1)

So if the length of A1 is less than 25 characters, A1 is returned.
 
Upvote 0
Hi,

Many thanks for your quick response.

I've tried that but get an error, it doesn't seem to like the comma after >=25 for some reason.
 
Upvote 0
Sorry, i left the = sign in by mistake:

=IF(LEN(A1)>=25,LEFT(A1,FIND("^^",SUBSTITUTE(A1," ","^^",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1),A1)
 
Upvote 0
UDF
Code:
Function RemoveCharsBeforeLastSpace(cell As Range) As String
    With CreateObject("VBScript.RegExp")
        .Pattern = "\s+\w+$"
        RemoveCharsBeforeLastSpace = .Replace(cell, "")
    End With
End Function
 
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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