[英]How to save Specific worksheets from a workbook using VBA?
Objective: 目的:
Conditions: 条件:
I've found VBA code that gets me half way to my goal. 我发现VBA代码可以使我达到目标的一半。 It saves ALL worksheets in the workbook but the file name is not dynamic with the current date.
它将所有工作表保存在工作簿中,但文件名不会随当前日期动态变化。
Current Code: 当前代码:
Private Sub SaveWorksheetsAsCsv()
Dim WS As Excel.Worksheet
Dim SaveToDirectory As String
Dim DateToday As Range
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "S:\test\"
For Each WS In ThisWorkbook.Worksheets
Sheets(WS.Name).Copy
ActiveWorkbook.SaveAs Filename:=SaveToDirectory & 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
There are several issues with your code: 您的代码有几个问题:
i) There is no reason to save the format or name of your current workbook. i)没有理由保存您当前工作簿的格式或名称。 Just use a new workbook to save the CSVs that you want.
只需使用新的工作簿来保存所需的CSV。
ii) You were copying each worksheet in the book, but not copying it anywhere. ii)您正在复制书中的每个工作表,但没有将其复制到任何地方。 This code was actually saving the same workbook with the name of each sheet.
该代码实际上是使用每个工作表的名称保存同一工作簿。 Copying the worksheet doesn't paste it anywhere and doesn't actually tell the saving function only to use parts of the document.
复制工作表不会将其粘贴到任何地方,实际上并不会告诉保存功能仅使用文档的某些部分。
iii) To put the date in the name, you just need to append it to the save name string, as below. iii)要将日期放入名称中,只需将其附加到保存名称字符串中,如下所示。
Dim myWorksheets() As String 'Array to hold worksheet names to copy
Dim newWB As Workbook
Dim CurrWB As Workbook
Dim i As Integer
Set CurrWB = ThisWorkbook
SaveToDirectory = "S:\test\"
myWorksheets = Split("SheetName1, SheetName2, SheetName3", ",")
'this contains an array of the sheets.
'If you want more, put another comma and then the next sheet name.
'You need to put the real sheet names here.
For i = LBound(myWorksheets) To UBound(myWorksheets) 'Go through entire array
Set newWB = Workbooks.Add 'Create new workbook
CurrWB.Sheets(Trim(myWorksheets(i))).Copy Before:=newWB.Sheets(1)
'Copy worksheet to new workbook
newWB.SaveAs Filename:=SaveToDirectory & Format(Date, "yyyymmdd") & myWorksheets(i), FileFormat:=xlCSV
'Save new workbook in csv format to requested directory including date.
newWB.Close saveChanges:=False
'Close new workbook without saving (it is already saved)
Next i
CurrWB.Save 'save original workbook.
End Sub
It seems to me that in that code was a lot of unnecessary stuff but the most important part was almost ready. 在我看来,该代码中有很多不必要的东西,但最重要的部分几乎已经准备就绪。 Try this:
尝试这个:
Sub SaveWorksheetsAsCsv()
Dim WS As Worksheet
Dim SaveToDirectory As String
SaveToDirectory = "C:\tmp\"
Application.DisplayAlerts = False
For Each WS In ThisWorkbook.Worksheets
WS.SaveAs Filename:=SaveToDirectory & Format(Now(), "yyyymmdd") & "_" & WS.Name & ".csv", FileFormat:=xlCSV
Next
Application.DisplayAlerts = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.