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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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