![]() |
|
![]() |
|
Thread Tools | Display Modes |
|
#1
|
||||
|
||||
![]()
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. |
#2
|
|||
|
|||
![]()
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. |
#3
|
||||
|
||||
![]()
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 |
#4
|
|||
|
|||
![]()
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?
|
#5
|
||||
|
||||
![]()
<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. |
#6
|
|||
|
|||
![]()
Yes, that is it precisely! Thanks for explaining it better than I did! I'll do some google searching too.
|
#7
|
||||
|
||||
![]()
Last night I was thinking about my old Basic programming, and I believe maybe I was wrong. It's coming back to me, vaguely, that on output statements (repeat "output"), a comma between arguments causes the data to be separated by a tab—either a literal <Tab> character or maybe just a specified number of spaces, I'm not sure—but that a semicolon writes out the data all jammed up against each other, leaving it up to the programmer to decide where to write spaces and so forth.
That's for output. For input, I've an even vaguer notion that commas are more of a problem, maybe an insoluble one, though I'm reluctant to believe it. Unless someone comes along and straightens us out, I'm about to suggest that you drop the Open, Input and Close statements and use the FileSystemObject. For one thing, it gives you more options when checking drives, paths, dates and other such properties of files. For another—and this is probably the biggest reason—I know how it works ![]() If you decide you're willing to go that route, or at least to think about it, check out again the code I wrote below and look at Microsoft's on-line documentation here. The point is that the ReadLine method reads an entire line of data at once, leaving it to you to break it up into pieces, which in this case is just what you want. |
![]() |
|
![]() |
||||
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 |