将多个 Excel 工作表转换为 .csv

将多个 Excel 工作表转换为 .csv

我找到了这个宏,用于将工作簿中的多个 Excel 工作表转换为单独命名的 .csv 文件,但不知道如何从转换后的文件名中删除工作簿名称。我确信它很简单,但我对 VBA 一无所知。

Sub exportcsv()
Dim ws As Worksheet
Dim path As String

path = ActiveWorkbook.path & "\" & Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1)
For Each ws In Worksheets
ws.Copy
ActiveWorkbook.SaveAs Filename:=path & "_" & ws.Name & ".csv", leFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close False
Next
End Sub

答案1

改变

ActiveWorkbook.SaveAs Filename:=path & "_" & ws.Name & ".csv"

ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.path & "\" & ws.Name & ".csv"

相关内容