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:
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.
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 LongfLen = 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).
If the PathName that is supplied to the VBA FileLen function does not relate to an existing file, you will get the error: