Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Using Seek In a Macro

Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Using Seek In a Macro.

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.

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.

Related Tips:

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

izal    17 Feb 2016, 08:35
Sorry. I mean by the function:
VBA.Seek(filenumber)
izal    17 Feb 2016, 08:33
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
Ray Austin    27 Jan 2015, 14:14
Barry,
Thank you, that's spot on
Ray
Barry    26 Jan 2015, 10:24
@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.
Ray Austin    25 Jan 2015, 15:55
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
Barry    24 Jan 2015, 05:40
@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.
Ray Austin    24 Jan 2015, 05:21
Can I set it so it goes to the end of the current text ?
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.