使用VB将excel工作表保存为CSV文件名+工作表名称

我很新的VB编码,我试图保存多个Excel文件工作表到CSV,我不知道这样做多张,但我find了一种方法来做单个文件。 我发现在这个网站上的代码是非常有用的,我想要做的,只有问题是文件与工作表名称保存,但我想保存与原始文件和工作表名称,如filename_worksheet name ,我试图自己做这件事,但不断发生错误,请问我是否做错了?

我正在使用的代码如下所示:

  Public Sub SaveWorksheetsAsCsv() Dim WS As Excel.Worksheet Dim SaveToDirectory As String Dim CurrentWorkbook As String Dim CurrentFormat As Long CurrentWorkbook = ThisWorkbook.FullName CurrentFormat = ThisWorkbook.FileFormat ' Store current details for the workbook SaveToDirectory = "H:\test\" For Each WS In ThisWorkbook.Worksheets WS.SaveAs SaveToDirectory & WS.Name, xlCSV Next Application.DisplayAlerts = False ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat Application.DisplayAlerts = True ' Temporarily turn alerts off to prevent the user being prompted ' about overwriting the original file. End Sub 

我想这是你想要的…

 Sub SaveWorksheetsAsCsv() Dim WS As Excel.Worksheet Dim SaveToDirectory As String Dim CurrentWorkbook As String Dim CurrentFormat As Long CurrentWorkbook = ThisWorkbook.FullName CurrentFormat = ThisWorkbook.FileFormat ' Store current details for the workbook SaveToDirectory = "H:\test\" For Each WS In ThisWorkbook.Worksheets Sheets(WS.Name).Copy ActiveWorkbook.SaveAs Filename:=SaveToDirectory & ThisWorkbook.Name & "-" & WS.Name & ".csv", FileFormat:=xlCSV ActiveWorkbook.Close savechanges:=False ThisWorkbook.Activate Next Application.DisplayAlerts = False ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat Application.DisplayAlerts = True ' Temporarily turn alerts off to prevent the user being prompted ' about overwriting the original file. End Sub 

我有一个类似的问题。 工作表中的数据我需要保存为单独的CSV文件。

这是我的代码后面的命令button


 Private Sub cmdSave() Dim sFileName As String Dim WB As Workbook Application.DisplayAlerts = False sFileName = "MyFileName.csv" 'Copy the contents of required sheet ready to paste into the new CSV Sheets(1).Range("A1:T85").Copy 'Define your own range 'Open a new XLS workbook, save it as the file name Set WB = Workbooks.Add With WB .Title = "MyTitle" .Subject = "MySubject" .Sheets(1).Select ActiveSheet.Paste .SaveAs "MyDirectory\" & sFileName, xlCSV .Close End With Application.DisplayAlerts = True End Sub 

这适用于我:-)

这是你正在尝试?

 Option Explicit Public Sub SaveWorksheetsAsCsv() Dim WS As Worksheet Dim SaveToDirectory As String, newName As String SaveToDirectory = "H:\test\" For Each WS In ThisWorkbook.Worksheets newName = GetBookName(ThisWorkbook.Name) & "_" & WS.Name WS.Copy ActiveWorkbook.SaveAs SaveToDirectory & newName, xlCSV ActiveWorkbook.Close Savechanges:=False Next End Sub Function GetBookName(strwb As String) As String GetBookName = Left(strwb, (InStrRev(strwb, ".", -1, vbTextCompare) - 1)) End Function 

上面的代码完美地处理一个小缺陷。 生成的文件不会以.csv扩展名保存。 – 2天前的Tensigh

我添加了以下代码,并将其保存为csv文件。 感谢这一点code.It一切按预期工作。

 ActiveWorkbook.SaveAs Filename:=SaveToDirectory & ThisWorkbook.Name & "-" & WS.Name & ".csv", FileFormat:=xlCSV 

最好的方法是loggingmacros并执行确切的步骤,看看它生成的VBA代码。 你可以去replace你想要的通用的(即文件名和东西)