Find character counting from the right

Sheridi

Board Regular
Joined
Nov 20, 2015
Messages
76
I know I've seen this done but for the life of me can't figure it out.

I want to find the first space in a text string (in a cell) counting from the right.

ie.... in Cell A1 I have "now is the time"

I want a formula to tell me that the first space from the right is at position 5.

Thanks!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Probably a better way to do this, but this works...
=LEN(A1)-FIND("@",SUBSTITUTE(A1," ","@",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))+1
 
Upvote 0
I thinkit only works if there is only one space. Here's a VBA function:

Code:
Function TEST(r As Range) As Long
    For i = Len(r.Value) To 1 Step -1
        If Mid(r.Value, i, 1) = " " Then
            TEST = i
            Exit Function
        End If
    Next i
End Function
 
Upvote 0
I thinkit only works if there is only one space. Here's a VBA function:

the formula I suggested, while a bit cumbersome, returns 5 in the provided sample, and works on other samples I created...
A​
B​
1​
now is the time
5​
2​
now is the too
4​
3​
now one t
2​
4​
now two two aabbccdd
9​
B1=LEN(A1)-FIND("@",SUBSTITUTE(A1," ","@",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))+1
copied down
 
Upvote 0
Solution
the formula I suggested, while a bit cumbersome, returns 5 in the provided sample, and works on other samples I created...
A​
B​
1​
now is the time
5​
2​
now is the too
4​
3​
now one t
2​
4​
now two two aabbccdd
9​

<tbody>
</tbody>

B1=LEN(A1)-FIND("@",SUBSTITUTE(A1," ","@",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))+1
copied down


This does it!

Thanks so much.
 
Upvote 0
I thinkit only works if there is only one space. Here's a VBA function:

Code:
Function TEST(r As Range) As Long
    For i = Len(r.Value) To 1 Step -1
        If Mid(r.Value, i, 1) = " " Then
            TEST = i
            Exit Function
        End If
    Next i
End Function
There is a much simpler VBA solution than that available...
Code:
Function Test(S As String) As Long
  Test = InStrRev(S, " ")
End Function
However, these functions do not give the answer the OP wanted. The OP wanted the count from the right side of the text, our functions above give the count from the left side. Although the OP has a formula solution, for completeness sake, here is the UDF version...
Code:
Function LastSpace(S As String) As Long
  If S Like "* *" Then LastSpace = Len(S) - InStrRev(S, " ") + 1
End Function
 
Upvote 0
Perhaps

Code:
Function LastSpaceFromRight(aString As String) As Long
    LastSpaceFromRight = 1 + Len(Split(StrReverse(aString), " ")(0))
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,016
Members
448,936
Latest member
almerpogi

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