Like any other programming language,
Excel VBA, too, supports file operation. Some of the basic file handling
process are creating a file, Writing Data into a file, Reading Data from a
file, Opening an existing file, closing a file, deleting data from the file
etc. File operation helps us to store data and retrieve them whenever required.
I will address these operation one by one.
Creating a File
This is the first and foremost activity
among all other operation. A file must be in place before an operation is
conducted. Hence, file creation is an important activity. Syntax to open a file
is
Open pathname For Output|Binary|Append|Input [Access
access] [lock] As [#] filenumber
Open, For and As # are required
Keywords. Full Path-name, Access, lock are optional. If we do not give full path name, the file will be present in the folder where the workbook is.
Keyword For, takes either Output (The
file will be created and will accept data), Binary (The file will be created
and will accept Binary Values), Append (The file already exist and new values
will be appended at the end) or Input (The file has been opened for reading
data from it) as it argument. Based on our requirements, we will select one of
the arguments.
The access
factor is an optional argument. But, if we wish to use it, we need to
append keyword Access before specifying access
factor. It can have either Write
(New file has been created and we need to write values to it) or Read Write (New is created/Existing
file is opened and we need to read and write values to it).
The lock factor is optional. It is
to control and guide behavior of other processes/applications when the file is
being used by the current application. It can take either Shared (Can access this file), Lock
Write (Cannot write when the file is opened) or Lock Read Write (Cannot Read/Write when it opened) value.
Filenumber can be any number between 1 and 511. FreeFile() function returns the next
available number. This can be used at times and is very handy when one is
working with multiple files.
Closing a
File
We should not forget to close an open file after we
are done with reading or writing into a file. This will free memory.
Close #filenumber
Writing Data
into File
There are two commands, Print and Write, to write data
into a file. The format of the same is shown below.
Write #filenumber, [outputlist]
Print #filenumber, [outputlist]
Here is an example how data is written into file
Spc() and tab () are used to add spaces and tab in
between the data. The data will be updated in example.dat file as shown below
Reading data
from the file
In order to read data from the file, we first need to
open the Pre-existing file. Input command is used to read data
from the file. Syntax for Input is
Input #filenumber,
VarName
(Reads data one
by one)
Or
Line Input #filenumber,
VarName
(Reads entire
line)
When the data is written using Write command, it would be good to read it either by Input or Line Input command. Line
Input is handy when data is appended with Print command. Following example uses Input command to read data from the file. Subsequently, I will also show the difference between Input and Line Input commands.
EOF(filenumber) determines
whether the end of file has been reached.When executed, this returns Boolean value i.e. True/False.