Find the nth occurence of a character in a text string

ctrlaltdel

Board Regular
Joined
Dec 1, 2002
Messages
60
Does someone know of either a formula or VBA code to locate and give the position of the nth occurence of a character in a text string in a cell?

For example:

If cell C5 = "Colorado - 1995 - Math - State Deptartment"

How might I get the character position of the 3rd hyphen without doing a nested formula?

Thanks for any help on this.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Perfect! Thank you.

So to isolate all of the text after the third hyphen, I use the formula:

=MID(A1,FIND(CHAR(127),SUBSTITUTE(A1,"-",CHAR(127),3))+2,LEN(A1))

I know that there are no trailing spaces, but could add trim to this if necessary.

Does this sound like the easiest way?
 
Upvote 0
ctrlaltdel said:
Perfect! Thank you.

So to isolate all of the text after the third hyphen, I use the formula:

=MID(A1,FIND(CHAR(127),SUBSTITUTE(A1,"-",CHAR(127),3))+2,LEN(A1))

I know that there are no trailing spaces, but could add trim to this if necessary.

Does this sound like the easiest way?


+2 is risky. Why 3 -- because it's the last hyphen?
 
Upvote 0
There is always a space after the hyphen, so by using the +2, I get to the first letter of the word following.

Unfortunately I cannot be assured that the 3rd hyphen will be the last, so I need to extract all the text after the 3rd hyphen from the left.

Do you have a better solution?
 
Upvote 0
ctrlaltdel said:
There is always a space after the hyphen, so by using the +2, I get to the first letter of the word following.

Unfortunately I cannot be assured that the 3rd hyphen will be the last, so I need to extract all the text after the 3rd hyphen from the left.

Do you have a better solution?

=TRIM(RIGHT(C5,LEN(C5)-SEARCH(CHAR(127),SUBSTITUTE(C5,"-",CHAR(127),LEN(C5)-LEN(SUBSTITUTE(C5,"-",""))))))

would give you whatever is after the last hyphen.
 
Upvote 0
This worked for me, its a function rather than a macro.
Code:
Function AfterLastHyphen(celldata)
Dim i As Long
    AfterLastHyphen = ""
    For i = Len(celldata) To 1 Step -1
        If Mid(celldata, i, 1) = "-" Then Exit For
        AfterLastHyphen = Mid(celldata, i, 1) & AfterLastHyphen
    Next i
    AfterLastHyphen = Trim(AfterLastHyphen)
End Function
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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