
Tips.Net > ExcelTips Home > Files > Determining the Length of a Text File
Summary: When creating a macro to process a text file, you may need to know the length of that file. You can figure this out by using the LOF function, as described in this tip. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)
Several other ExcelTips have discussed opening, reading, writing, appending, and closing text files. Another VBA function associated with sequential text files is the LOF function. If used on an open file, it returns the length of the file, in bytes. In other words, you can determine the number of characters in the file. This can come in handy if you are processing a text file character by character. You can determine the length of the file and then read that many characters before you finish processing the file. The following code fragment is an example of how the LOF function is used:
Open "MyFile.Dat" for Input as #1 FileLen = LOF(1)
Tip #2517 applies to Microsoft Excel versions: 97 2000 2002 2003 2007
Save Time! You can have this tip (and several hundred just like it) in the ExcelTips annual archives. Imagine having over 400 tips available at your fingertips, in each annual volume.
Want to make Excel do even more? The way is easy when you know how to use macros. This great e-book makes it easy. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site