Search notes:

Excel: Export worksheets as CSV when workbook is saved

This piece of code demonstrates how this event can be used to export a workbook's worksheets 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

See also

The afterSave event of workbook.
Excel: Importing and displaying CSV data

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Microsof...', 1758201370, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Microsoft/Office/Excel/data-exchange_import-export/CSV/export-worksheets-when-workbook-is-saved(75): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78