View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default How to export a range to CSV

Actually, we can save a couple of lines and two variable...

Sub ExportSelectedRangeAsCSV()
Dim X As Long, FF As Long, S() As String
ReDim S(1 To Selection.Rows.Count)
For X = 1 To Selection.Rows.Count
S(X) = Join(WorksheetFunction.Transpose(WorksheetFunction . _
Transpose(Selection.Rows(X).Value)), ",")
Next
FF = FreeFile
Open "c:\temp\Data.cvs" For Output As #FF
Print #FF, Join(S, vbNewLine)
Close #FF
End Sub

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
I think this code should work for you (assuming a rectangular selection),
just change the path in the Open statement to the directory location where
you want to put the Data.csv file...

Sub ExportSelectedRangeAsCSV()
Dim X As Long, FF As Long, R As Long, S() As String, StrOut As String
R = Selection.Rows.Count
ReDim S(1 To R)
For X = 1 To R
S(X) = Join(WorksheetFunction.Transpose(WorksheetFunction . _
Transpose(Selection.Rows(X).Value)), ",")
Next
StrOut = Join(S, vbNewLine)
FF = FreeFile
Open "c:\temp\Data.cvs" For Output As #FF
Print #FF, StrOut
Close #FF
End Sub

--
Rick (MVP - Excel)


"krumme" wrote in message
...
Hi

I am fairly new to VBA programming, however I feel confident that
something like this should be possible.

I want to create a script that exports a range selection in Excel 2007
to a CSV file (data.csv) using comma as field separator.

I have no problem making the selection and everything, my problem is
how to get the export working with the comma separator. My Excel uses
semicolon as default!

I really hope you can help me?

Thanks!