如何从xls文件制作多个csv文件?

时间:2014-07-22 21:00:38

标签: excel excel-vba csv vba

  • 我有12张Excel文件。
  • 尝试将文件保存为CSV文件(Save As ...)时,我必须手动执行12次(分别针对每个工作表)。

有没有办法不手动执行或一步手动执行此操作?

1 个答案:

答案 0 :(得分:3)

我提供了来自代码的Export each sheet to a separate csv file版本

它将单个工作表保存到与主机工作簿(ActiveWorkbook)相同的路径,并使用从主机工作簿中使用的每个工作表选项卡的名称作为文件名。

Sub SaveAsCSV()
Dim objWB As Workbook
Dim objws As Worksheet

Set objWB = ActiveWorkbook

If Len(objWB.Path) = 0 Then
MsgBox ActiveWorkbook.Name & " is not saved. Please save file then re-run this code", vbCritical
Exit Sub
End If

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
End With

For Each objws In objWB.Sheets
  objws.Copy
  ActiveWorkbook.SaveAs objWB.Path & "\" & objws.Name & ".csv", xlCSV
  ActiveWorkbook.Close False
Next

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True
End With

End Sub