How to specify 'cut off point' for pulling data from within a cell

pete212

New Member
Joined
Aug 20, 2012
Messages
20
Hello, I have the below code that inserts data from a text file into cell A1, if the text file contains the string "Checking for drops":

Dim FileNum As Long, TotalFile As String
FileNum = FreeFile
Open "C\pete.txt" For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum , , TotalFile
Close #FileNum

If InStr(TotalFile, "Checking for drops") Then Range("A1") = Mid(Split(TotalFile, "Checking for drops")(1), 2
, 7190)

The 7190 shown in red is the number of characters it pastes into A1 after this string. The problem I have is that this character limit can vary each time the code runs. 7190 is the worst case sceanrio, so often A1 contains data beyond what I actually need.

Is there a way to define a cut off string, so that rather than defining a character limit i can pass a second string that tells it to stop when it reaches that string? E.G. "Checking for drops" is where I need it to start and "show spp" is where I need it to stop.

If its easier, instead we could query A1 for "show spp" directly and paste the contents into another cell, rather than trying to deal with it straight from the text file.

I was thinking in B1 I could do a "=FIND("show spp",A1)" to get the character number of where show spp appears and then in C1 a "=LEFT(A1,1,<b1>B1)" but just seeing if there is any easier way?</b1>
 
Last edited:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
A guess, but perhaps:
Rich (BB code):
If InStr(TotalFile, "Checking for drops") Then Range("A1") = Mid(Split(TotalFile, "Checking for drops")(1), 2, Mid(Split(TotalFile, "show spp")(1), 1))
 
Upvote 0

Forum statistics

Threads
1,223,054
Messages
6,169,834
Members
452,284
Latest member
TKM623

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top