Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Using Seek In a Macro.

Using Seek In a Macro

Written by Allen Wyatt (last updated June 8, 2019)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


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 halfway 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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9367) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Using Seek In a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Scaling Your Printing

If you want to cram more of your worksheet onto each page of a printout, one way to do it is by using scaling. Here's how ...

Discover More

Lines that Don't Change When You Type

Create a form in Word and you will invariably be faced with the need to places fill-in-the-blank lines in the document. ...

Discover More

Thoughts and Ideas on Significant Digits in Excel

Ruminations and reflections about significant digits in Excel. Includes examples of how significant digits can affect the ...

Discover More

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!

More ExcelTips (ribbon)

Triggering an Event when a Worksheet is Deactivated

One way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's ...

Discover More

Skipping Hidden Rows in a Macro

As your macro processes information in a worksheet, you may want to make sure that it skips over rows that are hidden. ...

Discover More

Writing a Macro from Scratch

Creating macros can help extend what you can do in Excel. If you work with macros, you know that creating macros from ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 3?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.