Return to VBA Code Examples

VBA LOF Function

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on July 19, 2021

LOF Description

Returns a Long representing the size, in bytes, of a file opened by using the Open statement.

LOF Syntax

In the VBA Editor, you can type  “LOF(” to see the syntax for the LOF Function:


The LOF function contains an argument:

FileNumber: A valid file number.

Examples of Excel VBA LOF Function

To test the LOF function, 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 LOF_Example()
    Open "D:\test.txt" For Input As #1
    MsgBox LOF(1)
    Close #1
End Sub

Then, it will show the following dialog.

It means the file size is 16 bytes.

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