Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract string from right till the space 4

Status
Not open for further replies.

Bilberry

Programmer
Dec 17, 2007
111
0
0
NL
Hi all,
Does anybody know how to extract a text till a space from the right?
For examle

1234 DF DDDDD

I want to extract only DDDDD

 
If your data is in cell A2, then

=TRIM(SUBSTITUTE(A2,TRIM(LEFT(SUBSTITUTE(TRIM(A2)," ",REPT(" ",99)),198)),""))

should extract any text after the second space

To find last word in any string (I have tried only to 5 words)

=IF(ISERR(FIND(" ",A2)),"",RIGHT(A2,LEN(A2)-FIND("*",SUBSTITUTE(A2," ","*",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))))))




Avoid Hangovers. Stay drunk.
 
Thannks a lot xlhelp. Im still busy with your formulas:

But what if i only have one word? We can have such values:

1234 DF DDDDD
DDFFKD
1123 JJ KKKKK

 
In case of error (no space inside) use whole text instead of space:
=IF(ISERR(FIND(" ",A2)),A2,RIGHT(A2,LEN(A2)-FIND("*",SUBSTITUTE(A2," ","*",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))))))

combo
 
Instead of a formula, would a little macro be a possibility?

If staring in A2 you have:[tt]
1234 DF DDDDD
DDFFKD
1123 JJ KKKKK[/tt]

This little macro:

Code:
Dim r As Integer
Dim s() As String

r = 2
Do While Range("A" & r).Value <> ""
    s = Split(Range("A" & r).Value, " ")
    Range("B" & r).Value = s(UBound(s))
    r = r + 1
Loop

puts this starting in B2:[tt]
DDDDD
DDFFKD
KKKKK
[/tt]
This macro will stop at the first empty cell in column A

Have fun.

---- Andy
 
or a user define function that you can use on your sheet...
Code:
Function LastWord(s As String) As String
    dim a
    a = Split(s, " ")
    LastWord = a(UBound(a))
End Function


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
As long as the last word contains fewer than 99 characters, you can return it with:
=TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",99)),99))

This formula will work even if the cell contains a single word (i.e. no spaces).
 
If the cell text ends with one or more spaces, my formula will not give the correct answer. The remedy is another TRIM:
=TRIM(RIGHT(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),99))

That formula will also work if the cell is blank, contains an empty string, or contains the Gettysburg Address.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top