将Excel里所有的Sheet页同时另存为单独的CSV文件 博客分类: VBA VBAExcelSheetCSV多个
程序员文章站
2024-02-13 12:35:04
...
Sub ExcelToCsvMain() '检查工作薄中有多少个工作表 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\" '判断文件目录是否存在 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:存在问题,无法指定代码页TextCodePage :Variant 类型,可选。不在美国英语版的 Microsoft Excel 中使用。