saving selecting columns to a txt file


Posted by john pedersen on February 08, 2002 10:22 PM

I would like to save selected columns to a txt file.
Then returning to the worksheet and save a different selected set of columns to a text file.

Posted by DK on February 09, 2002 5:53 AM

Hi John,

There are two ways you could do this:-

The first would be to use the file operation methods in VBA such as Write and Print. The second, and much simpler way is to use a macro to copy the selection to a new workbook and save that as text. Something like this will do the trick:-

Sub SaveColumns()
Dim rnge As Range, wb As Workbook

On Error GoTo ErrHandler

Application.ScreenUpdating = False
Set rnge = Selection
rnge.Copy
Set wb = Workbooks.Add
wb.Sheets(1).Range("a1").PasteSpecial xlPasteValues

wb.SaveAs "C:\temp\filename.txt", xlText
wb.Close False
Application.ScreenUpdating = True
Exit Sub


ErrHandler:
Application.ScreenUpdating = True
MsgBox "Error number - " & Err.Number & vbCrLf & "Error description - " & Err.Description
End Sub

HTH,
D

Posted by Mark W. on February 09, 2002 3:09 PM

Hide the columns that you don't want to save,
choose the File | Save As... menu command,
and use the "Formatted Text (Space delimited)
(*.prn)" Save as type.

Posted by john pedersen on February 10, 2002 9:33 PM

Thanks Mark for your help the example worked great.



Posted by john pedersen on February 10, 2002 9:34 PM

Thanks your example worked perfect too!