我目前使用的VBA宏将工作簿中的所有工作表复制并保存为.csv。下面显示的宏只使用工作表名称作为新文件名。
Sub ExportSheetsToCSV()
Dim xWs As Worksheet
Dim xcsvFile As String
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.Copy
xcsvFile = CurDir & "\" & xWs.Name & ".csv"
Application.ActiveWorkbook.SaveAs Filename:=xcsvFile, _
FileFormat:=xlCSV, CreateBackup:=False
Application.ActiveWorkbook.Saved = True
Application.ActiveWorkbook.Close
Next
End Sub
我的问题是,如何才能将一部分工作簿名称添加到开头?感谢
答案 0 :(得分:0)
例如(未经测试)
Sub ExportSheetsToCSV()
Dim xWs As Worksheet, wb As Workbook, wbCopy As Workbook
Dim xcsvFile As String
Set wb = Application.ActiveWorkbook
For Each xWs In wb.Worksheets
xWs.Copy
Set wbCopy = Application.ActiveWorkbook
'include the first 4 characters from the workbook name
xcsvFile = CurDir & "\" & Left(wb.Name, 4) & "_" & xWs.Name & ".csv"
wbCopy.SaveAs Filename:=xcsvFile, FileFormat:=xlCSV, _
CreateBackup:=False
wbCopy.Close False 'no save...
Next
End Sub