简体   繁体   English

Excel宏:遍历工作簿并从每个工作簿中打印单独的工作表

[英]Excel Macro: iterate through workbooks and print individual sheets from each

The steps I need to carry out are repetitive, but I'm not sure how to iterate through each workbook and then each sheet. 我需要执行的步骤是重复的,但是我不确定如何依次遍历每个工作簿和每个工作表。

My task is to: 我的任务是:

  1. Look in a folder: (Source folder) 在文件夹中查找:(源文件夹)
  2. Iterate through each workbook in that folder (Source file) 遍历该文件夹中的每个工作簿(源文件)
  3. Print each of the 4 worksheets (sheet name) in each workbook to a PostScript Printer (Printer Name/Path). 将每个工作簿中的4个工作表(工作表名称)中的每一个打印到PostScript打印机(打印机名称/路径)。
  4. Name the printed to file PS file = Source file+sheet name 命名打印文件PS文件=源文件+图纸名称
  5. Final PS output files placed in final folder (destination folder) 最终PS输出文件放置在最终文件夹(目标文件夹)中
  6. Original workbook closed and not saved. 原始工作簿已关闭且未保存。

I searched for iteration VBA/Macro and have seen some ideas, but I'm unsure how the code looks when it's working through workbooks and worksheets. 我搜索了VBA / Macro迭代,并看到了一些想法,但是我不确定代码在工作簿和工作表中的外观。

Also, the PS printer is done through Printing To File. 另外,PS打印机是通过“打印到文件”完成的。 Does this cause problems. 这会引起问题吗?

UPDATED WITH CODE I'VE TRIED SO FAR: 我尝试过用代码更新:

Sub Make_PS_Files()

Dim path2 As String, path3 As String

path2 = "Drive:\Source folder\"
path3 = " Drive:\Destination folder\"

Workbooks.Open Filename:=path2 + "File_Name.XLS"
Sheets("Specific_Sheet_Name1").Activate

  Application.ActivePrinter = "\\PRINTER NAME\LOCATION:"

  ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
   :=True, Prtofilename:=True
   ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name1.ps"


Sheets("Specific_Sheet_Name2").Activate

  Application.ActivePrinter = "\\VS PRINTER NAME\LOCATION:"

  ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
   :=True, Prtofilename:=True
   ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name2.ps"


Sheets("Specific_Sheet_Name3").Activate

  Application.ActivePrinter = "\\ PRINTER NAME\LOCATION:"

  ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
   :=True, Prtofilename:=True
   ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name3.ps"
ActiveWorkbook.Close


Sheets("Specific_Sheet_Name4").Activate

  Application.ActivePrinter = "\\ PRINTER NAME\LOCATION:"

  ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
   :=True, Prtofilename:=True
   ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name4.ps"
ActiveWorkbook.Close

End Sub

Apologies for not posting this last night when I posted. 抱歉昨晚我未发布此帖子。 It seems very long winded for what it is doing. 它正在做什么似乎很漫长。 I thought it could be polished a bit more so it is more universal and can be pointed at any workbook and any number of sheets. 我认为可以对其进行抛光,以使其更通用,并且可以指向任何工作簿和任何数量的图纸。

Not all the sheets have the Specific_Sheet_Name, so I would like to iterate through without reference to the name. 并非所有的工作表都具有Specific_Sheet_Name,因此我想遍历而不参考名称。

You can iterate through the workbooks in a folder using the FileSystemObject by using the Folder and File objects. 您可以使用FolderFile对象,使用FileSystemObject遍历文件夹中的工作簿。 (Remember to add a reference to the Microsoft Scripting Runtime ) (请记住要添加对Microsoft Scripting Runtime的引用)

Iterating through worksheets is as easy as a For Each over the Workbook.Sheets collection. 遍历工作表就像在Workbook.Sheets集合中For Each一样容易。

Finally, you can print the worksheet using the PrintOut method on each worksheet to print them. 最后,您可以在每个工作表上使用PrintOut方法打印工作表以进行打印。 Just make sure to set the PrintToFile parameter to true. 只要确保将PrintToFile参数设置为true即可。

If you need more direction than that, I suggest following @JMax's advice and post some of what you've tried already. 如果您需要更多指导,建议您遵循@JMax的建议并发布一些您已经尝试过的方法。

UPDATE 更新

To iterate through workbooks using FileSystemObject : 使用FileSystemObject遍历工作簿:

Sub Make_PS_Files()
  Dim fso As New Scripting.FileSystemObject
  Dim source As Scripting.Folder
  Dim wbFile As Scripting.File
  Dim book As Excel.Workbook

  Set source = fso.GetFolder("Drive:\Source folder\")
  For Each wbFile In source.Files
    If fso.GetExtensionName(wbFile.Name) = "xls" Then
      Set book = Workbooks.Open(wbFile.Path)
      ' Print out the workbook
    End If
  Next 
End Sub

And to iterate through the worksheets in a workbook: 并遍历工作簿中的工作表:

Dim sheet As Excel.Worksheet

For Each sheet in book.Sheets
  sheet.PrintOut Copies:=1, ActivePrinter:="\\Printer:", PrintToFile:=True, _
                 PrToFileName:=fso.BuildPath(destination, sheet.Name & ".ps")
Next

Keep in mind that this has no error handling, but I hope it helps. 请记住,这没有错误处理,但是希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel宏:从具有多个刷新功能的多个工作簿中复制工作表 - Excel Macro: Copy Sheets from Multiple Workbooks WITH Added Refresh Capability Pandas:遍历 DataFrame 列表并将每个导出到 Excel 表 - Pandas: Iterate through a list of DataFrames and export each to excel sheets 遍历一个目录中的每个excel文件,将单个工作表转换为CSV文件 - Iterate through every excel file in one directory, convert individual sheets to CSV files Excel宏可从多个工作簿写入单个工作簿 - Excel macro to write from multiple workbooks to a single Excel宏可基于多个值打印图纸 - Excel macro to print sheets based on multiple values 导入多个 excel 工作簿,每个工作簿包含 R 中的多个工作表 - Import multiple excel workbooks each containing multiple sheets in R 如何将 Excel 表格数据迭代到 DataGridView? - How to Iterate through Excel Sheets Data to DataGridView? Excel VBA宏:导入工作簿,复制列,并循环通过导入的工作簿 - Excel VBA Macro: Importing Workbooks, Copying Columns, And Looping Through Imported Workbooks 宏循环遍历多个 excel 文件并从每个文件下载数据 - Macro to loop through multiple excel files and download data from each 循环浏览Excel工作簿,从每个工作簿复制一定范围的单元格并粘贴到主工作簿 - Looping through Excel workbooks, copying a range of cells from each and pasting to a master workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM