SaveAllSheetsAsCSV Macro
Jump to navigation
Jump to search
A SaveAllSheetsAsCSV Macro is an Excel macro that saves all of an Excel sheets to CSV files.
- Example(s):
Public Sub SaveAllSheetsAsCSV()
On Error GoTo Heaven
'
' each sheet reference
Dim Sheet As Worksheet
' path to output to
Dim OutputPath As String
' name of each csv
Dim OutputFile As String
'
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
'
' Save the file in current director
OutputPath = ThisWorkbook.Path
'
If OutputPath <> "" Then
Application.Calculation = xlCalculationManual
'
' save for each sheet
For Each Sheet In Sheets
'
OutputFile = OutputPath & Application.PathSeparator & Sheet. Name & ".csv"
'
' make a copy to create a new book with this sheet
' otherwise you will always only get the first sheet
'
Sheet.Copy
' this copy will now become active
ActiveWorkbook.SaveAs Filename:=OutputFile, FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
Next
'
Application.Calculation = xlCalculationAutomatic
'
End If
'
Finally:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
'
Exit Sub
'
Heaven:
MsgBox "Couldn't save all sheets to CSV." & vbCrLf & _
"Source: " & Err.Source & " " & vbCrLf & _
"Number: " & Err.Number & " " & vbCrLf & _
"Description: " & Err.Description & " " & vbCrLf
'
GoTo Finally
End Sub
- Counter-Example(s):
- an MS Word Macro.
- See: Visual Basic.