![]() |
|
![]() |
|
Thread Tools | Display Modes |
|
#1
|
||||
|
||||
![]()
Hi, omaha. So you want to start processing the contents of the file when you get to a line that's equal to "1990", and stop when you get to one that has ... well, something else? Sure, shouldn't be a problem. But what do you want to do with the data? In your program you're displaying the line number where "1990" first appears, and that's a start. But once there, do you want to copy the data to another file, save it in an Excel worksheet, display it one line at a time in a MsgBox, what?
|
#2
|
|||
|
|||
![]()
Thanks for the response! My goal is to have the values fill certain cells. I figure I can do that using Range pretty good. Although the data will be taken from different parts of the text files, the place on the Excel sheets should be about the same. I know the number of possible data points that the files could have, so I'd leave enough space for the biggest text file size. My goal is to extract data from the rather arcane software output file and make it into a spreadsheet. I would do it just one text file at a time.
|
#3
|
||||
|
||||
![]()
You didn't ask any actual questions
![]() Or maybe you need a hint: You can see that you're not sure how to proceed, but you can't see what questions to ask. So let me try leading: 1) Does the value "1900" go into your worksheet, or is it just a trigger that tells you the data you want to save will be on the next line? 2) Once you have a line with data you want to save, do you want to put the whole line into a worksheet cell, or just part of it? If part of it, what VBA statements will you use to extract just the part you want? 3) Once you have the data you want to put in the worksheet, how will you decide which row and column to store them in? And what VBA statement will you use for that purpose? See what you can do with those questions; that'll either clarify for you how to write the program, or clarify for me what kind of help you need. Maybe both. |
#4
|
|||
|
|||
![]()
My bad! I should have clarified my post more.
How do I pull line by line while ignoring delimiters? And how do I have the import start after the string I am looking for and then finish when it comes across another predefined string? In answer of the questions: 1. 1900 does not need to go in. Just after it. 2. All of 1 line=1 cell. That is my main difficulty; Excel automatically tries to delimit it using the code above. 3. I will figure out the exact cells later, but they will always be the same cells. |
#5
|
||||
|
||||
![]()
Out of habit, for I/O in VB I use the FileSystemObject rather than the native Basic statements for I/O—the Open, Input and Close statements, I mean [see footnote]. But judging by your results, what you've done so far will work fine, so unless you decide you want to switch to the fso, let's go with what you have so far. Let's start with your program as it is so far:
Code:
Open Fname For Input As #1 jr = 0 Do While Not EOF(1) Input #1, A jr = jr + 1 If A = "1990" Then MsgBox jr Loop Close 1 Code:
Open Fname For Input As #1 ' First find the starting statement. Do While Not EOF(1) Input #1, A If A = "1990" Then Exit Do Loop If EOF(1) then Exit Sub ' "1990" did not appear ' Now handle the subsequent data. Do While Not EOF(1) Input #1, A ' insert statements here to put the value of A into various cells. Loop Close 1 Code:
Open Fname For Input As #1 fData = False Do While Not EOF(1) Input #1, A If fData Then ' insert statements here to put the value of A into various cells. ElseIf A = "1990" Then fData = True End If Loop Close 1 None of the above takes into account the ending trigger; I expect you can work that out yourself, given the above hints. But feel free to ask further if you need more help. --- [footnote:] The FileSystemObject gives you a lot of control over your PC's drives, folders and files. But the differences in this logic are pretty minor. Here's your program, as I would have written it: Code:
Open Fname For Input As #1 jr = 0 Do While Not EOF(1) Input #1, A jr = jr + 1 If A = "1990" Then MsgBox jr Loop Close 1 Code:
Set fo = fso.OpenTextFile(Fname, 1) 'ForReading jr = 0 Do While Not fo.AtEndOfStream A = fo.ReadLine jr = jr + 1 If A = "1990" Then MsgBox jr Loop fo.Close |
#6
|
|||
|
|||
![]()
Thank you. I am almost there! I got the endpoint figured out and it works great! My last question: My data is still imported with delimiters, IE a comma in the text file would mean that the data from one line goes to two separate cells. I only want lines in the text file to be imported, with the comma included. What can I do to ignore all the delimiters?
|
#7
|
||||
|
||||
![]()
<Chuckle> Remember I said I'm not used to using native Basic commands for I/O? That's getting in the way, here. I'm not sure, but I think you're saying this:
Quote:
If no one answers, and I can't find it, then you can switch to using the FileSystemObject, which I know works. But first let's see whether you or I can find, or some lurker can tell us, the right form of the Input statement. |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
VBNation | Word VBA | 2 | 02-08-2013 07:14 AM |
![]() |
errtu | Word | 1 | 01-31-2013 02:09 PM |
Excel Fomula to search for a string and display value from different column | zeeshanbutt | Excel | 1 | 07-29-2012 12:48 AM |
![]() |
francis | Excel | 1 | 10-06-2011 01:43 PM |
Extract numbers from a text string | aleale97 | Excel | 4 | 02-10-2011 10:33 AM |