Thursday, September 27, 2012

VBA String Manipulations - Parsing Strings - 1

Finding a string within another string and extracting it

Sub FindAstringWithinAnotherStringAndExtractIt()

' ______________________________________
'
' This works both in Excel and Word VBA
' ______________________________________

Dim String_to_Parse, Front_String, Back_String, Temp_String, The_Parsed_String As String

String_to_Parse = "Front-of-string---Middle-of-String-To-Be-Extracted---Back-of-String"

Front_String = "Front-of-string---"
Back_String = "---Back-of-String"

Temp_String = Replace(String_to_Parse, Front_String, "")
The_Parsed_String = Replace(Temp_String, Back_String, "")

MsgBox The_Parsed_String

' ______________________________________
'
'The result will be: Middle-of-String-To-Be-Extracted
' ______________________________________

End Sub

No comments:

Post a Comment

You may comment or show me other VBA tricks, but don't rest assured I'll always reply because I only have 24 hours in a day's hard work, and only a few minutes a week to update this blog... I'll try my best though...