Steve
I was referring to VBA help. There are a few commands that you can use for
dealing with text files. Some that I use are
Open
Close
Input
Print
Write
Line Input
From Excel, open the VBE (alt-F11), then open VBA help (F1). Go to the
index tab and look for Line Input. On the help page, near the top, there
will be a See Also link which shows related commands. For example, under
Line Input #, there is the Input #. Under Input #'s See Also are commands
like Open and Close.
I could explain these commands to you, but I'd just have to look in help
(like I do every time I use them because I can't remember the syntax) so I
figured you could go straight to the source.
Here's some comments on the code example I provided to help you get started.
Here I'm putting the file name in a variable
FreeFile is a function that gets the next available file number. The file
number is just a way for VBA to keep track of open text files. You could
also write this as
Fnum = 1
If you know there are no other files open and 1 won't conflict with
anything. I always use FreeFile, but not everyone does.
This is the Open statement. I'm opening the file I specified above for
Input and I'm telling VBA to use Fnum as the identifier. I can then refer
to Fnum later and it will know what I'm talking about. You can open text
files for Input, Output, Append and a few other things. I chose Input,
because I knew that I would only be reading in from the file, not writing to
the file.
This is a counter that I use to identify how many lines of the text file I
found that matched.
The EOF function returns true when the pointer is at the end of the text
file. I want to run everything between Do and Loop until I get to the end.
Line Input inputs the current line of the text file to a variable. I set up
a variable called sLine to hold the text that it reads in. Input # would
read the next field, not the next whole line (although in some cases they
are the same). If you had a comma-separated text file, Input # would read
to the next comma. That can be useful depending on what you're trying to
do. In either case, the pointer gets moved to the end of whats read. So
after it reads the first line, the pointer is at the start of the second
line. After it reads the last line, EOF becomes true and the loop stops.
I have the current line in a variable calles sLine. Here I use the Instr
function to determine if the word "text" exists in that line.
If the word "text" exists in that line, I write it to a cell
then increment the counter so if it finds another line with "text" it will
right to the next cell below - and not overwrite the first one.
This closes the text file. You always want a close statement whenever you
open a file. Otherwise the file will remain open.
Hopefully that gives you a good start. There's not a ton of commands
available for working with text files, so it shouldn't take you long to
learn them. However, they can be tricky (like the difference between Write
and Print) so it takes some experimentation to see what works for your
particular situation.
Good luck, and be sure to post back if you need further clarification.