The VBA FileLen Function

Related Functions:
VBA FileDateTime
VBA GetAttr

Description

The VBA FileLen function returns the length (in bytes) of a supplied file. The returned value is a Long data type.

The syntax of the function is:

FileLen( PathName )

Where the PathName argument provides the path and file name of the file that you want the length of.

Note that the FileLen function returns the length of the file when it was last saved, so if you have the file open and modified (but not saved), the length of the file may change when you save it.


VBA FileLen Function Example

In the example below, the VBA FileLen function is used to return the length of a supplied text file:

' Return the length of the file data.txt.
Dim fLen As Long
fLen = FileLen( "C:\Users\John\Documents\data.txt" )
' fLen is now equal to 18292.

After running the above VBA code, the variable fLen is equal to 18292. I.e. the length of the supplied file is 18292 bytes (18.292 KB).


VBA FileLen Function Error

If the PathName that is supplied to the VBA FileLen function does not relate to an existing file, you will get the error:

Run-time error '76': Path not found

VBA Run Time Error 76 Message Box