Return to VBA Code Examples

VBA Line Input Statement

Line Input Description

Reads a single line from an Open sequential file and assigns it to a string.

Line Input Syntax

Line Input #FileNumber, VarName

The Line Input statement contains 2 arguments:

FileNumber: Any valid file number.

VarName: Valid Varient or String variable name.

Examples of Excel VBA Line Input Statement

To test the Line Input Statement, create a text file “test.txt” on the D drive.(D:\test.txt) Assume that the content of the file is as following.

1 2 3
xy z

Please run the following code.

Sub LineInput_Example()
    Dim strLine As String
    Dim strContent As String
    Open "D:\test.txt" For Input As #1 ' Open file.
    Do While Not EOF(1) ' Loop until end of file.
        Line Input #1, strLine ' Read line into variable.
        strContent = strContent & strLine
    Close #1 ' Close file.
    MsgBox strContent
End Sub

Then, the result will be as following.

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