[英]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: 我的任务是:
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. 您可以使用Folder和File对象,使用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.