export n columns to a .csv

  • Thread starter Thread starter Laima
  • Start date Start date
L

Laima

Is there a way to save only selected columns from a
worksheet into a .csv file without using copy & paste? Is
there a way to do this programmatically or via a macro?
 
Laima,

This macro will export the selected range as a CSV

Sub ExportCSV()
Dim fs, f
OutFile = Application.GetSaveAsFilename(FileFilter:="CSV (Comma delimited)(*.csv), *.csv")
If OutFile = False Then Exit Sub
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(OutFile, 2, -2)
For Rcount = 1 To Selection.Rows.Count
For CCount = 1 To Selection.Columns.Count
Outline = CStr(Selection.Cells(Rcount, CCount).Text)
f.Write Outline
If CCount <> Selection.Columns.Count Then f.Write ", "
Next
f.Write vbCrLf
Next
f.Close
Set f = Nothing
Set fs = Nothing
End Sub

Dan E
 
Back
Top