MrExcel Publishing
Your One Stop for Excel Tips & Solutions

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?



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.

Damon