Excel 2007 Power Programming with VBA (Mr. Spreadsheets Bookshelf)

VBA contains a number of statements that allow low-level manipulation of files. These Input/Output (I/O) statements give you much more control over files than Excel's normal text file import and export options.

A file can be accessed in any of three ways:

Because random and binary access files are rarely used with VBA, this chapter focuses on sequential access files, which are accessed sequentially. In other words, your code starts reading from the beginning of the file and reads each line sequentially. For output, your code writes data to the end of the file.

Note  

The method of reading and writing text files discussed in this book is the traditional data-channel approach. Another option is to use the object approach. The FileSystemObject object contains a TextStream object that can be used to read and write text files. The FileSystemObject object is part of the Windows Scripting Host. As I mention earlier, this scripting service is disabled on some systems because of the possibility of transferring a virus.

Opening a text file

VBA's Open statement (not to be confused with the Open method of the Workbooks object) opens a file for reading or writing. Before you can read from or write to a file, you must open it.

The Open statement is quite versatile and has a rather complex syntax:

Open pathname For mode [Access access] [lock] _ As [#]filenumber [Len=reclength]

Reading a text file

The basic procedure for reading a text file with VBA consists of the following steps:

  1. Open the file by using the Open statement.

  2. Specify the position in the file by using the Seek function (optional).

  3. Read data from the file (by using the Input , Input # , or Line Input # statements).

  4. Close the file by using the Close statement.

Writing a text file

The basic procedure for writing a text file is:

  1. Open or create the file by using the Open statement.

  2. Specify the position in the file by using the Seek function (optional).

  3. Write data to the file by using the Write # or Print # statements.

  4. Close the file by using the Close statement.

Getting a file number

Most VBA programmers simply designate a file number in their Open statement. For example:

Open "myfile.txt" For Input As #1

Then you can refer to the file in subsequent statements as #1 .

If a second file is opened while the first is still open, you would designate the second file as #2 :

Open "another.txt" For Input As #2

Another approach is to use VBA's FreeFile function to get a file handle. Then you can refer to the file by using a variable. Here's an example:

FileHandle = FreeFile Open "myfile.txt" For Input As FileHandle

Determining or setting the file position

For sequential file access, it's rarely necessary to know the current location in the file. If for some reason you need to know this, you can use the Seek function.

Statements for reading and writing

VBA provides several statements to read and write data to a file.

Excel's Text File Import and Export Features

Excel supports three types of text files:

When you attempt to open a text file with the Office Open command, the Text Import Wizard might appear in order to help you delineate the columns. If the text file is tab-delimited or comma-delimited, Excel usually opens the file without displaying the Text Import Wizard. If the data is not interpreted correctly, close the file and try renaming it to use a .TXT extension.

The Text to Columns Wizard (accessed by choosing Data Data Tools Text to Table) is identical to the Text Import Wizard but works with data stored in a single worksheet column.

 

Three statements are used for reading data from a sequential access file:

Two statements are used for writing data to a sequential access file:

Категории