Several other tips in other issues of ExcelTips discuss opening, reading, writing, appending, and closing text files from within a macro. Another command associated with sequential text files is the Seek command. If used on an open file, Seek positions the internal file pointer at a specific character number in the file. The following code fragment is an example of how it is used:
Open "DOSTEXT.DAT" for Input as #1 iFileLen = LOF(1) Seek 1, iFileLen / 2
These program lines use the LOF function to determine the length of the file. The last line then positions the internal file pointer half way through the file. All subsequent reading or writing of the file will take place from that position.
You can also use Seek as a function to determine your current position within a text file. This is what this code does:
iCurPos = Seek(1)
This command leaves the internal file pointer where it was, but sets iCurPos to a value representing how many characters into the file the pointer is. The iCurPos value is the position at which all subsequent reading and writing of the file will take place.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2475) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Using Seek In a Macro.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Strings are used quite frequently in macros. You may want to swap the contents of two string variables, and you can do so ...
Discover MoreYou can use macros to process information in your worksheets. You may want to use that macro to apply the italic ...
Discover MoreDo you need your macro to select all the visible worksheets (and just the visible ones)? It's not as easy as it sounds, ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2016-02-17 08:35:50
izal
Sorry. I mean by the function:
VBA.Seek(filenumber)
2016-02-17 08:33:22
izal
It might be not important but good to know that they are two different "thing".
One is a VBA Function: BA.Seek(position)
This function gives us the position of cursor in file.
The other is a VBA statement: Seek filenumber, position
This statement moves the cursor to the position
2015-01-27 14:14:33
Ray Austin
Barry,
Thank you, that's spot on
Ray
2015-01-26 10:24:31
Barry
@Ray
If you use:
Seek(1),LOF(1)
this will position the pointer at the end of the file whereupon you can insert /write further text.
Alternatively, if you open a file in "Append" mode then text will be automatically added to the end of the file.
2015-01-25 15:55:26
Ray Austin
As it is a text file I expect text to be added from time to time. I would like it to display the end of all the existing text, so the new text would follow in chronological order.
ie the opposite of the stupid MS e mail standard, where subsequent entries are at the top and the whole cannot be easily read in proper sequence.
Thank you
2015-01-24 05:40:37
Barry
@Ray
You need to define what you mean by"current" text. Do you mean the current word, sentence or paragraph?
Excel sees the files as just a long string of characters spaces and punctuation are just other characters no different to a,b,c,d..... etc.
2015-01-24 05:21:24
Ray Austin
Can I set it so it goes to the end of the current text ?
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments