VBA FreeFile For Foolproof File IO PDF
VBA FreeFile For Foolproof File IO PDF
com
The examples in this tutorial will show you how to use VBA FreeFile for your next Excel project. FreeFile is
nice, but it’s meaningless if you don’t truly know how to manipulate files with VBA. To become a true file I/O
expert, you should look at our comprehensive VBA File I/O Cheat Sheet filled with 50+ tips for working with
files and over 30 file input/output macro examples.
Close #file1
Close #file2
End Sub
https://wellsr.com/vba/2016/excel/vba-freefile-for-foolproof-file-IO/ 1/4
17/2/2020 VBA FreeFile for Foolproof File IO - wellsr.com
In the above example, the variable file1 is given a value of 1 since no other files were opened in the macro
before issuing the FreeFile command. By storing the results of the FreeFile command to a variable, you’re
able to call that variable in your Open statements and in your Print or Write statements.
By the time you issue the FreeFile command the second time, the command returns a value of 2 and this
integer is stored in the variable file2 . A value of 2 is returned since the value of 1 is already taken
by file1 and this file is already open.
Sub FreeFile_Demo2()
Dim file1 as Integer, file2 As Integer
In this example, the FreeFile command returns a value of 1 twice, because we already closed file #1. This
means #1 is available to use again!
https://wellsr.com/vba/2016/excel/vba-freefile-for-foolproof-file-IO/ 2/4
17/2/2020 VBA FreeFile for Foolproof File IO - wellsr.com
Sub FreeFile_Demo3()
Dim file1 as Integer, file2 As Integer
Close #1
Close #3
Close #file1
Close #file2
End Sub
Just as we expected, the VBA FreeFile function marches through, realizes files #1 and #3 are open, so the
first time FreeFile is invoked it assigns a value of #2. The second time it’s called, FreeFile skips over #3 since
#3 is already opened and it assigns the next free file number, which is #4.
FreeFile Arguments
The VBA FreeFile function in Excel (and other Office Applications) doesn’t require any arguments, although it
does accept an optional argument. By default, the FreeFile function returns the next integer between 1 and
255 that isn’t already opened by your macro. However, if you pass the FreeFile function a value of 1, it will
search for the next available file between numbers 256 and 511:
file1 = FreeFile(1)
Proper VBA file IO etiquette is to use a file number between 1 and 255 for files you do not want accessible to
other applications and to use file numbers between 256 and 511 for files that may be used by other
applications.
Don’t let this confuse you. Nine times out 10, just enter the FreeFile command without any arguments, like
this:
file1 = FreeFile
https://wellsr.com/vba/2016/excel/vba-freefile-for-foolproof-file-IO/ 3/4
17/2/2020 VBA FreeFile for Foolproof File IO - wellsr.com
A good example of where I use FreeFile in my applications is when I have users selecting files via a dialog box
and I want to write an error log or status log. Instead of picking an arbitrarily large integer for my file number,
I use the FreeFile function to simply pick the next free file number.
That’s all for this tutorial. When you’re ready to take your VBA to the next level, subscribe using the form
below.
https://wellsr.com/vba/2016/excel/vba-freefile-for-foolproof-file-IO/ 4/4