Excel VBA – 打印(arrays)

快速的问题,为什么下面的VBA打印将无法正常工作…如果我有它单独设置(Sheet1.PrintOut)打印出来罚款,但如果我这样做数组(Sheet1,Sheet2,Sheet3).PrintOut它不起作用。 谁能解释为什么?

Sub printnow() Dim Sheet1 As Worksheet Dim Sheet2 As Worksheet Dim Sheet3 As Worksheet With ThisWorkbook Set Sheet1 = .Sheets("database1") Set Sheet2 = .Sheets("database2") Set Sheet3 = .Sheets("database3") 'Setting up the print setup Sheet3.PageSetup.PaperSize = xlPaperLegal Sheet3.PageSetup.Orientation = xlPortrait 'Print End With Array(Sheet1,Sheet2.Sheet3).PrintOut Copies:=1 End Sub 

Array函数调用返回包含Sheet1,Sheet2和Sheet3的Variant() 。 一个Variant()没有一个.PrintOut方法 – 它不是一个对象。 如果你想数组的每个对象上调用.PrintOut ,只需循环它:

 Dim sheet As Variant For Each sheet In Array(Sheet1, Sheet2, Sheet3) sheet.PrintOut Copies:=1 Next 

您可以将一系列图纸名称传递给工作Sheets ,并处理它们。

Sheets(Array("database1", "database2", "database3")).Printout Copies:=1

您也可以使用Sheet.Name:

Sheets(Array(Sheet1.Name, Sheet2.Name, Sheet3.Name)).Printout Copies:=1

或者你可以使用工作表的索引:

Sheets(Array(1,2,3)).Printout Copies:=1