Solved

search and get a string from a line

Posted on 2013-06-19
3
275 Views
Last Modified: 2013-06-23
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
0
Comment
Question by:alam747
3 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 39261500
Text = Mid(Source, Instr(Source, "LN_"))

Where Source = "abc LN_defghijkelmopqrstuv"

Kevin
0
 
LVL 80

Expert Comment

by:byundt
ID: 39262160
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.
0
 

Author Closing Comment

by:alam747
ID: 39269630
Thanks a lot
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now