Sunday, April 08, 2007

Run-time Error 55!!! File already open

Run-time Error 55 File already open. This is one of the common error in file handling. It is difficult to avoid if you handle multiple files and use direct file numbers

Sub TextFile_Write_Error()

' ---------------------------------------------------------------
' Written By Shanmuga Sundara Raman for
' ---------------------------------------------------------------

' ------------------------
' Code with Errors
' ------------------------
Open "d:\VBADudExamples\TextFile1.txt" For Output As #1 ' Open file for output.

Print #1, "TextFile1"

Open "d:\VBADudExamples\TextFile2.txt" For Output As #1 ' Open file for output.

Close #1 ' Close file.

Close #1 ' Close file.

' ------------------------
' Workaround for the Errors
' ------------------------
' Use FreeFile to get a file number that is not used

iF1 = FreeFile ' Returns an Integer representing the next file number available for use by the Open statement.
Open "d:\VBADudExamples\TextFile1.txt" For Output As #iF1 ' Open file for output.

Print #iF1, "TextFile1"

iF2 = FreeFile
Open "d:\VBADudExamples\TextFile2.txt" For Output As #iF2 ' Open file for output.

Close #iF2 ' Close file.

Close #iF1 ' Close file.

' ------------------------------------------------
' Excel VBA, Run-time Error 55, File already open
' ------------------------------------------------

End Sub


  1. Anonymous11:18 PM

    It Worked

  2. I typically use the intrinsic freefile function to get unique file numbers. But I have a problem with a procedure that opens two files, one for input, the other for output. Both use file numbers created with free file. Both files are closed at the end of the routine using Close, such as
    Close #fhIn
    Close #fhOut

    When I call the procedure a second time, the open statement for the input file executes without a problem, but the open statement for the output file generates the Error 55, file already open. This file was closed properly and it is not being opened anywhere else between calls to this procedure.

  3. Thomas, I am experiencing the exact same issue. A subsequent Open statement to a file that was previously closed (both time opened as output) and I get this error. I abort the program then restart with no changes and it runs fine the second time (happens like this every time). The file was definitely closed using "Close #FileNum" and FreeFile() used to reopen at a later time (usually within 1 to 10 minutes).

    Any fixes or workarounds let me know. Thanks.


  4. This thread seems to be very old however the topic is always accurate. When you define to freefile variables - depending on the speed (clock-time) of your CPU the integer value for the two variables will be the same especially if you're running your app. under a fast CPU. Since I had been suffering from this error for a long while I did the following:

    Fr1 = freefile
    Fr2 = freefile + 1

    It works for me like a charm.

    Steve (

    1. Anonymous2:55 AM

      freefile works very well



Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.