Simple Recursive VBA String Replacement Function


Public Function cut_and_paste(strObject As String, strCut As String, strPaste As String) As String
‘strObject is the base string
‘strCut is the string being cut out of strObject
‘strPaste is the string replacing all instances of strCut

Dim strFINAL As String

If InStr(strObject, strCut) > 0 Then
strFINAL = Mid(strObject, 1, InStr(strObject, strCut) – 1) & _
strPaste & cut_and_paste(Mid(strObject, InStr(strObject, strCut) + Len(strCut)), strCut, strPaste)
strFINAL = strObject
End If

cut_and_paste = strFINAL

End Function

Comments are closed.

Log in