2016-10-03 80 views
0

隨着WBVBA - 打印工作簿中的工作表文件夾

Set Sh2 = .Sheets("sheet2) 
    With Sh2.PageSetup 
     .PrintArea = "$B$2:$S$80" 
     .PaperSize = xlPaperLegal 
    End With 

Set Sh3 = .Sheets("sheet3") 
    With Sh3.PageSetup 
     .PrintArea = "$B$2:$M$104" 
     .PaperSize = xlPaperLegal 
     .Orientation = xlPortrait 
    End With 

Set execsum1 = .Sheets("sheet4") 
    With execsum1.PageSetup 
     .PrintArea = "$B$7:$N$63" 
     .PaperSize = xlPaperLegal 
     .Orientation = xlLandscape 
     .PrintTitleRows = "$B$2:$N$6" 
    End With 

Set execsum2 = .Sheets("sheet5") 
    With execsum2.PageSetup 
     .PrintArea = "$B$64:$N$106" 
     .PaperSize = xlPaperLegal 
     .Orientation = xlLandscape 
     .PrintTitleRows = "$B$2:$N$6" 
    End With 
    'ActiveSheet.PrintPreview 

Set noi1 = .Sheets("sheet6") 
    With noi1.PageSetup 
     .PrintArea = "$B$10:$N$44" 
     .PaperSize = xlPaperLegal 
     .Orientation = xlLandscape 
     .PrintTitleRows = "$B$2:$N$8" 
     .FitToPagesTall = 1 
    End With 

Set noi2 = .Sheets("sheet7") 
    With noi2.PageSetup 
     .PrintArea = "$B$46:$N$192" 
     .PaperSize = xlPaperLegal 
     .Orientation = xlLandscape 
     .PrintTitleRows = "$B$2:$N$8" 
     '.FitToPagesWide = 1 
     .FitToPagesTall = 1 
    End With 

End With 

Dim sheet As Variant 
For Each sheet In Array(execsum1, execsum2, Sh2, Sh3, noi1, noi2) 
    sheet.PrintOut Copies:=1 
Next 

    'Save and Close Workbook 
     'wb.Close SaveChanges:=False 

    'Ensure Workbook has closed before moving on to next line of code 
     DoEvents 

    'Get next file name 
     myFile = Dir 
    Loop 

'Message Box when tasks are completed 
    MsgBox "Task Complete!" 

ResetSettings: 
    'Reset Macro Optimization Settings 
    Application.EnableEvents = True 
    Application.Calculation = xlCalculationAutomatic 
    Application.ScreenUpdating = True 

End Sub 

大家好,我是新來的VBA和我想打印每文件夾中找到工作簿中的工作表6 /頁。 Execsum1和execsum2來自具有不同打印區域的相同工作表;與noi1和noi2同樣的故事。當我運行代碼時,它將打印出第二個指定頁面兩次(execsum2和noi2)。爲什麼不打印execsum1/noi1,如果可能的話,我怎樣才能使代碼更有效率?謝謝。

回答

1

它將兩次打印相同的頁面,因爲您不是在將PageSetup從第一個版本更改爲第二個版本之間打印工作表。您在此處收集工作表參考...

Set execsum1 = .Sheets("Exec Summary") 
'... 
Set execsum2 = .Sheets("Exec Summary") 

...彼此相同。一個工作表只有1 PageSetup,所以當你做到這一點...

For Each sheet In Array(execsum1, execsum2, Sh2, Sh3, noi1, noi2) 
    sheet.PrintOut Copies:=1 
Next 

...你把它設置爲過去的事情。

只是完全跳過循環並單獨打印每一個。循環播放它們絕對沒有好處。

With execsum1.PageSetup 
    .PrintArea = "$B$7:$N$63" 
    .PaperSize = xlPaperLegal 
    .Orientation = xlLandscape 
    .PrintTitleRows = "$B$2:$N$6" 
End With 
execsum1.PrintOut Copies:=1 '<--- After each With block. 

如果要簡化代碼,只需解壓出來的共同.PageSetup到一個函數,並通過一切作爲一個參數(注意,這只是一個例子 - 我不包括你的一切」重新使用)。即:

Private Sub PrintCustomRange(sheet As Worksheet, area As String, title As String, _ 
          orient As XlPageOrientation, paper As XlPaperSize) 
    With sheet.PageSetup 
     .PrintArea = area 
     .PaperSize = paper 
     .Orientation = orient 
     If Len(title) > 0 Then .PrintTitleRows = title 
    End With 
    .PrintOut Copies:=1 
End Sub 

然後調用它像這樣:

PrintCustomRange Sheets("Proforma NOI"), "$B$46:$N$192", "$B$2:$N$8", xlLandscape, xlPaperLegal 
+0

@wowmoo - 這聽起來就像是與打印機驅動程序。 – Comintern

相關問題