Save all Sheet pages in Excel as separate CSV files at the same time

 

Sub ExcelToCsvMain()
'Check how many sheets are in the workbook
   Dim i, sheet_count, sheet_name As String
   sheet_count = Sheets.Count
   'msgbox sheet_count
   For i = 1 To sheet_count
       sheet_name = Sheets(i).Name
       Sheets(sheet_name).Select
       Call ExportSelectionToCSV
   Next
   Sheets(1).Select
End Sub
Function ExportSelectionToCSV()
   Dim wks As Worksheet
   Dim newWks As Worksheet
   Dim bookPath As String
   bookPath = ThisWorkbook.Path
   bookPath = bookPath +"\TEMP\"
   'Check if the file directory exists
   If Dir(bookPath, 16) = Empty Then
       MkDir bookPath
   End If
   
   For Each wks In ActiveWindow.SelectedSheets
       wks.Copy 'to a new workbook
       Set newWks = ActiveSheet
       With newWks
           Application.DisplayAlerts = False
           .Parent.SaveAs Filename:=bookPath & .Name, _
                FileFormat:=xlCSV
           Application.DisplayAlerts = True
           .Parent.Close savechanges:=False
       End With
   Next wks
End Function

 PS: There is a problem, the code page cannot be specified TextCodePage: Variant type, optional. Not used in the US English version of Microsoft Excel.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326515748&siteId=291194637