Runtime Error 54: “Bad file mode,” explained and fixed

Error 54, “Bad file mode”, means a statement tried to use a file in a way its open mode forbids. A file opened For Input cannot be written to, a file opened For Output cannot be read, and random-access statements do not work on sequential files. The statement and the Open disagree, and error 54 is VBA enforcing the disagreement. It is always a code bug, usually a mode chosen by habit rather than by what the code actually does with the file.

For people who just run the program

  1. Expect it at one consistent step. The macro opens the file the same way every run, so the failure repeats at the same action; note which one.
  2. Do not try a different file first. The mode is chosen by the code, not the file, so every file fails identically. Trying another file only confirms that.
  3. Check if the tool has separate import and export modes. Using an export routine on a step that needs to read back its own file is a typical way users meet this error in packaged tools.
  4. Report the step and the tool's mode if it shows one. That pairing is what the maintainer needs.

For people who write the code

  1. Match the Open mode to every statement used on the file. Read-back after writing requires Append cannot read either; a file that is both written and read in one session belongs in Random or Binary mode, or must be closed and reopened between phases.
  2. Open path For Output As #fn   ' write-only: Input #fn here is error 54
    Print #fn, "header"
    Close #fn
    Open path For Input As #fn    ' close and reopen to change direction
    Line Input #fn, firstLine
  3. Use Get/Put only on Random and Binary files. Record-number operations against a sequential file are bad file mode even when the direction (read versus write) seems right.
  4. Audit shared Open helpers. A helper that opens every file For Append because most callers append will hand error 54 to the one caller that reads. Make the mode a parameter with an honest default.
Append mode is write-only, despite the friendly name. Code that opens a log file For Append and later tries to read the log back in the same session hits error 54. Reading requires Input, Random, or Binary.

If it still happens

If the mode looks right, check that the file number variable still refers to the file you think it does: a reused number after an earlier Close and Open in another procedure silently retargets every statement. Log the number and mode together at the Open and at the failure.

Common questions

Which modes allow both reading and writing? Random and Binary. Sequential modes are one-directional: Input reads, Output and Append write. Switching direction on a sequential file requires closing and reopening it in the other mode.

Can error 54 be caused by the file itself? No. The file's contents are irrelevant; error 54 is about the mismatch between the open mode and the statement. It fires before any data is touched.

Related errors

Runtime Error 52 · Runtime Error 55 · Runtime Error 59