簡體   English   中英

如何在 Excel 中使用 VBA 打印所有工作表? 我的代碼的行為不像我認為的那樣

[英]How do I print all sheets using VBA in Excel? My code doesn't behave like I think it should

我有一個 Excel 工作簿,只有一張開始。 然后,有一個宏從該工作表中獲取數據並為第一張工作表中的每一行創建一個新工作表。 它創建的工作表數量是動態的,並且會隨着時間的推移或根據我們設置的過濾器而增加或減少。 例如,假設它現在產生大約 20 個新工作表。

我有另一個宏,旨在打印所有新創建的工作表,但這就是我遇到問題的地方。 宏首先隱藏第一張紙,然后它應該是 select 所有可見紙, select 所有這些紙上的范圍,最后打印選擇。

(旁注:我通常使用“選擇”而不是工作表或工作簿,因為打印區域不會在紙上居中。)

在我的腦海中,這應該為每張紙打印一頁,但我無法讓它正常運行。 當我通過手動操作 go 記錄宏時,一切正常——打印命令顯示它要打印 19 頁。 太好了,但是當我播放相同的宏時。 它只想打印任何處於活動狀態的工作表,而不是 rest? 任何想法:這是我的代碼:

'Declare variables
Dim ws As Worksheet

'Temporarily hide the Customer List so the rest of the workbook can print
Worksheets("Customer List").Visible = False

'Select all visible sheets
For Each ws In Sheets
If ws.Visible Then ws.Select False
Next

'Select the range of cells on whatever sheets is active
Range("A1:J49").Select

'Promp user for which printer to use
Application.Dialogs(xlDialogPrinterSetup).Show

'Page setup
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.25)
    .RightMargin = Application.InchesToPoints(0.2)
    .TopMargin = Application.InchesToPoints(0.25)
    .BottomMargin = Application.InchesToPoints(0.25)
    .HeaderMargin = Application.InchesToPoints(0.05)
    .FooterMargin = Application.InchesToPoints(0.05)
    .PrintHeadings = False
    .PrintGridlines = False
    .PrintComments = xlPrintNoComments
    .PrintQuality = 600
    .CenterHorizontally = True
    .CenterVertically = True
    .Orientation = xlPortrait
    .Draft = False
    .PaperSize = xlPaperLetter
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
    .PrintErrors = xlPrintErrorsDisplayed
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    .ScaleWithDocHeaderFooter = True
    .AlignMarginsHeaderFooter = True
    .EvenPage.LeftHeader.Text = ""
    .EvenPage.CenterHeader.Text = ""
    .EvenPage.RightHeader.Text = ""
    .EvenPage.LeftFooter.Text = ""
    .EvenPage.CenterFooter.Text = ""
    .EvenPage.RightFooter.Text = ""
    .FirstPage.LeftHeader.Text = ""
    .FirstPage.CenterHeader.Text = ""
    .FirstPage.RightHeader.Text = ""
    .FirstPage.LeftFooter.Text = ""
    .FirstPage.CenterFooter.Text = ""
    .FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True

'Print all visible sheets
Selection.PrintOut Copies:=1, Collate:=True

'Unhide Customer List after printing and select
Worksheets("Customer List").Visible = True
Worksheets("Customer List").Select
Range("A1").Select

End Sub

我自己還是個新手,但如果我要嘗試這個,我會根據張數將它設置在 For/Next 循環和 go 中。 如果您的“客戶列表”是第一頁,那么您只需繞過它。

AllSheets = Application.Sheets.Count

Application.Dialogs(xlDialogPrinterSetup).Show

For i=2 to AllSheets  '//Assuming sheet 1 is the Customer List
Sheets(i).Range("A1:J49").Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.25)
    .RightMargin = Application.InchesToPoints(0.2)
    .TopMargin = Application.InchesToPoints(0.25)
    .BottomMargin = Application.InchesToPoints(0.25)
    .HeaderMargin = Application.InchesToPoints(0.05)
    .FooterMargin = Application.InchesToPoints(0.05)
    .PrintHeadings = False
    .PrintGridlines = False
    .PrintComments = xlPrintNoComments
    .PrintQuality = 600
    .CenterHorizontally = True
    .CenterVertically = True
    .Orientation = xlPortrait
    .Draft = False
    .PaperSize = xlPaperLetter
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
    .PrintErrors = xlPrintErrorsDisplayed
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    .ScaleWithDocHeaderFooter = True
    .AlignMarginsHeaderFooter = True
    .EvenPage.LeftHeader.Text = ""
    .EvenPage.CenterHeader.Text = ""
    .EvenPage.RightHeader.Text = ""
    .EvenPage.LeftFooter.Text = ""
    .EvenPage.CenterFooter.Text = ""
    .EvenPage.RightFooter.Text = ""
    .FirstPage.LeftHeader.Text = ""
    .FirstPage.CenterHeader.Text = ""
    .FirstPage.RightHeader.Text = ""
    .FirstPage.LeftFooter.Text = ""
    .FirstPage.CenterFooter.Text = ""
    .FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
'Print it
Selection.PrintOut Copies:=1
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM