Return to VBA Code Examples

VBA Open Statement

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on July 19, 2021

Open Description

Enables input/output (I/O) to a file.

Open Syntax

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

The Open statement contains 6 arguments:

pathname: String expression that specifies a file name; may include directory or folder, and drive.

mode: Keyword specifying the file mode: Append, Binary, Input, Output, or Random. If unspecified, the file is opened for Random access.

access: [Optional] Keyword specifying the operations permitted on the open file: Read, Write, or Read Write.

lock: [Optional]. Keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.

filenumber: A valid file number in the range 1 to 511, inclusive. Use the FreeFile function to obtain the next available file number.

reclength: [Optional] Number less than or equal to 32,767 (bytes). For files opened for random access, this value is the record length. For sequential files, this value is the number of characters buffered.

Examples of Excel VBA Open Statement

The following code opens the file in sequential-input mode.

Open "MyFile" For Input As #1 
    ' Close before reopening in another mode. 
Close #1

The following example opens the file in Binary mode for writing operations only.

Open "MyFile" For Binary Access Write As #1 
    ' Close before reopening in another mode. 
Close #1


VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as

Learn More!

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples