简体   繁体   English

如何使用VBA从工作簿中保存特定工作表?

[英]How to save Specific worksheets from a workbook using VBA?

Objective: 目的:

  1. To save specific worksheets in a workbook as unique CSV files 将工作簿中的特定工作表另存为唯一的CSV文件

Conditions: 条件:

  1. To save specific worksheets (plural) from a workbook that contains both the specific worksheets and extraneous worksheets (eg to save specific 10 out of 20 available worksheets) 若要从包含特定工作表和无关工作表的工作簿中保存特定工作表(复数个)(例如,从20个可用工作表中保存特定10个)
  2. Insert the current date into the CSV's file name in order to avoid overwriting files currently in the save folder (this VBA is run daily) 将当前日期插入CSV的文件名中,以避免覆盖当前保存文件夹中的文件(此VBA每天运行)
  3. File name syntax: CurrentDate_WorksheetName.csv 文件名语法:CurrentDate_WorksheetName.csv

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.

相关问题 如何使用VBA将工作簿中的特定工作表另存为唯一的PDF文件 - How to save specific worksheets in a workbook as unique PDF file using VBA 使用VBA根据条件将特定的命名工作表保存在工作簿中 - Saving specific named worksheets in workbook based on criteria using VBA Excel VBA-将所有工作表从特定工作簿复制到活动工作簿中 - Excel VBA - Copying all Worksheets from a specific workbook into an active workbok 如何编写VBA代码以删除工作簿中所有工作表的特定列? - How to write the VBA code to delete specific columns for all worksheets in a workbook? Excel VBA:如何在同一工作簿中合并特定的工作表? - Excel VBA: How to consolidate specific worksheets in the same workbook? VBA 为工作簿中的特定工作表设置循环 - VBA set a Loop for specific worksheets in a workbook 如何使用VBA用户窗体循环工作簿中的多个工作表 - How to Loop through several worksheets in a Workbook Using a VBA UserForm 如何使用工作表CodeName从特定工作簿(而不是“ ThisWorkbook”)中获取工作表对象? - How can I get a worksheet object from a specific workbook (not 'ThisWorkbook') using the worksheets CodeName? 如何将工作表移动到新的工作簿VBA - How to move worksheets to a new workbook vba 如何将 VBA 代码应用于工作簿中的所有工作表 - How to apply VBA code to all worksheets in the workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM