Archive of Mr Excel Message Board

Back to Excel VBA archive index
Back to archive home

Save selected cells to text file?

Posted by Mark P on January 08, 2002 8:26 AM
Is there a command that will automatically save a SELECTED group of cels to a text file? Bear in mind, while it'll always be the same text file (i.e. same name, location), the selected area will vary from one usage to another, so it's best if the range is not hardcoded into a macro.

Any ideas?

Check out our Excel VBA Resources

Re: Save selected cells to text file? Can do.

Posted by Damon Ostrander on January 08, 2002 12:36 PM
Hi Mark,

To write a selected group of cells to a file is fairly easy using VBA file I/O, as the following snipped of code illustrates.

Dim i As Long
Open "c:\temp\SavedCells.txt" For Output As #1
For i = 1 To Selection.Cells.Count
Print #1, Selection.Cells(i).Value
Next i
Close 1

Happy computing.


This archive is from the original message board at
All contents © 1998-2004
Visit our online store to buy searchable CD's with thousands of VBA and Excel answers.
Microsoft Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.