Excel: Export worksheets as CSV when workbook is saved
This piece of code demonstrates how this event can be used to export a workbook'sworksheets as CSV when the workbook is saved.
option explicit
private isSaving as boolean
private sub workbook_afterSave(byVal ok as boolean)
if isSaving then
exit sub
end if
isSaving = true
application.displayAlerts = false
dim sh as workSheet
for each sh in me.sheets
debug.print "Saving " & sh.name
sh.copy
'
' The workbook created by sh.copy becomes the new active workbook.
'
activeWorkbook.saveAs filename := me.path & "\CSV\" & sh.name & ".csv", fileFormat := xlCSV , createBackup := false
activeWorkbook.close
next sh
application.displayAlerts = true
isSaving = false
msgBox "CSVs were saved"
end sub
private sub workbook_open()
isSaving = false
end sub