问题
I would like to change the file print setting in Excel to go directly to VBA and print some of the 26 worksheets while printing the workbook. Most of the active worksheets should print, but a few of the worksheets should not print. The workbook is a template and the number of active and printed worksheets will change with each new workbook created. Lastly, should the VBA code go in the "ThisWorkbook" section?
回答1:
I would use something like
Sub PrintSheets ()
Dim Wks1 as Worksheet: set Wks1=Sheets("SheetName1")
Wks1.PrintOut Copies:=1, Collate:=True
'[...] and so on with your sheets
Where the no of copies can be changed To fully automatize the code I usualy create a new sheet called "Macro keys" where I store the sheets' name and copy count (i.e. A1= "SheetName1", B1="5" , A2="SheetName2", B2="10"). Thus:
Sub PrintSheets ()
Dim MacroKeys as Worksheet: Set MacroKeys = Sheets("Macrokeys")
SheetName1 = Macrokeys.Range("A1").Value
SheetName1_CopyCount = Macrokeys.Range("B1").Value
Sheet(SheetName1 ).PrintOut Copies:= SheetName1_CopyCount, Collate:=True
'[...] and so on with your sheets
End Sub
Hope this helps!
来源:https://stackoverflow.com/questions/19158209/how-do-i-change-the-file-print-setting-on-excel-to-use-vba-to-print-several-page