Excel VBA - delete string content up to *word* Excel VBA - delete string content up to *word* vba vba

Excel VBA - delete string content up to *word*


Here you go:

Dim s As Strings = "Emily has wild flowers. They are red and blue."Dim indexOfThey As IntegerindexOfThey = InStr(1, s, "They")Dim finalString As StringfinalString = Right(s, Len(s) - indexOfThey + 1)


Simple example of dropping all text before value in string.

Sub Foo()    Dim strOrig As String    Dim strReplace As String    strOrig = "The Quick brown fox jumped over the lazy dogs"    strReplace = "jumped"    MsgBox (DropTextBefore(strOrig, strReplace))End SubPublic Function DropTextBefore(strOrigin As String, strFind As String)    Dim strOut As String    Dim intFindPosition As Integer    'case insensitive search    'could made it so that case sensitivity is a parameter but this gets the idea across.    If strOrigin <> "" And strFind <> "" Then        intFindPosition = InStr(UCase(strOrigin), UCase(strFind))        strOut = Right(strOrigin, Len(strOrigin) - (intFindPosition + Len(strFind) - 1))    Else      strOut = "Error Empty Parameter in function call was encountered."    End If    DropTextBefore = strOutEnd Function


If the word is fixed, like "They" in the above example, you can simply do

  1. CTRL + H (Replace)
  2. *They (your word with a star)

in the Find box. The star * is a wildcard character which can be called as - of anything before or after (if added at end) the word.

Cautious: Take care when you have duplicate words in the same cell.