Skip to content

Instantly share code, notes, and snippets.

@martinctc
Last active March 1, 2019 21:27
Show Gist options
  • Save martinctc/fc6ed292c77e9f13ba186c049a834235 to your computer and use it in GitHub Desktop.
Save martinctc/fc6ed292c77e9f13ba186c049a834235 to your computer and use it in GitHub Desktop.
Save Worksheets in Excel file to CSV files (not my code) #Excel
Sub SaveWorksheetsAsCsv()
Dim WS As Excel.Worksheet
Dim SaveToDirectory As String
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "H:\test\"
For Each WS In ThisWorkbook.Worksheets
Sheets(WS.Name).Copy
ActiveWorkbook.SaveAs Filename:=SaveToDirectory & ThisWorkbook.Name & "-" & WS.Name & ".csv", FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
ThisWorkbook.Activate
Next
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat
Application.DisplayAlerts = True
' Temporarily turn alerts off to prevent the user being prompted
' about overwriting the original file.
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment