Link to home
Start Free TrialLog in
Avatar of alam747
alam747

asked on

search and get a string from a line

Hi Experts,

I want search and get a string from a sentence.
for example, the string contain the word "LN_" but LN_ is not in the begining of the string it can be like "abc LN_defghijkelmopqrstuv"  or "abcdefg LN_jkdjslajlkdjljflkjl"
I want to get the portion of the string start with LN_ up to the end of the string.
from above two strings I want to get "LN_defghijkelmopqrstuv" and "LN_jkdjslajlkdjljflkjl"

Please advise VBA code to do that.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is worth noting that you'll get a runtime error if LN is not in the string when combining MID with InStr. To avoid this, use three statements:
Text = ""
i = InStr(1, Source, "LN")
If i > 0 Then  Text = Mid(Source, i)

The first statement (clearing the value of Text variable) is required if you are returning values from more than one cell. Without it, if the second (or later) cell doesn't contain LN, then Text would repeat its preceding value.
Avatar of alam747
alam747

ASKER

Thanks a lot