Skip to content

[OT] VBA to save excel as csv

3 messages · Hadley Wickham, Henrique Dallazuanna, Eik Vettorazzi

#
Hi all,

This is a little off-topic, but it is on the general topic of getting
data in R.  I'm looking for a excel macro / vba script that will
export all spreadsheets in a directory (with one file per tab) into
csv.  Does anyone have anything like this?

Thanks,

Hadley
#
Hi Hadley,
in VBA you can do sth. like

Sub save_all_csv()
 oldName = ThisWorkbook.FullName
 curpath = ThisWorkbook.Path
 
  For Each ws In ActiveWorkbook.Worksheets
    ws.SaveAs Filename:=curpath & "\" & ws.Name & ".csv", 
FileFormat:=xlCSV, CreateBackup:=False
 Next
 'bring old file back
 Workbooks.Open Filename:=oldName
 ThisWorkbook.Close savechanges:=False
End Sub

My Excel (2000) has no SaveCopyAs - method for other file formats than 
xls, so I'm using .SaveAs, which saves the file and replaces the current 
worksheet with the saved one - thats why the code for bringing back the 
starting file
hth.


Hadley Wickham schrieb: