excelvbastringtext-extraction

How to extract a word in a string between two similar characters" with VBA?


I need to extract Wööörd_03 from this string:

"https://Word01.com/Word_02/Wööörd_03/Word_04/Word_05=0"

My code doesn't, cause I get different results:

Sub ExtractWord()

Dim sString As String, pString As String, iString As Long, jString As Long

sString = "https://Word01.com/Word_02/Wööörd_03/Word_04/Word_05=0"

iString = InStr(sString, "/")
jString = InStrRev(sString, "/")
pString = Mid(sString, iString + 21, jString - iString - 29)

MsgBox pString

End Sub

Does anyone know a better way to ALWAYS extract the word between the 4th and 5th slashes?

Thanks


Solution

  • Look into SPLIT()

    pString = Split(sString,"/")(4)