簡體   English   中英

excel VBA,嘗試打印區域?

[英]excel VBA, trying to print an area?

目前,我正在制作一個大型工作簿,試圖獲取該工作簿,因此在每個月末我運行一個宏來生成新工作表。

我已經把這個排序了。

接下來是一個宏,該宏使用戶可以選擇僅打印工作表中的相關信息。

這有點復雜! 由於不應打印數據中間的某些列,但是我仍然希望它們在所有其他時間都可見。

我的解決方案:

(請注意,我對VBA還是很陌生,這對我來說很復雜,所以我只使用了宏記錄器)

我將其分為兩個步驟,一個是通過按圖紙上的“創建打印區域”按鈕調用的宏,另一個是由“發送至打印機”按鈕調用的宏

創建打印區域宏-隱藏所有不需要的列,然后突出顯示“ $ B $ 1:$ Y $ 567”中的所有單元格,然后將其設為打印區域。

發送到打印機宏-將打印區域發送到打印機。 它具有A3大小的設置,適合寬度的列,以$ 1:$ 1作為打印標題,否則只是我認為的默認設置。 然后我取消隱藏運行“打印區域宏”所隱藏的單元格。

問題:到目前為止,第一個宏似乎正在工作,而大體上第二個宏似乎正在工作。 但是,盡管以正確的尺寸,正確的列數和正確的打印標題進行打印,它仍將打印工作表中的所有行。 (即在567行處不中斷)。 這不是一個大問題,因為在567之后我只有10行數據,但這有點煩人,因為這意味着生成了一個多余的頁面,而且看起來有些混亂,理想情況下,我可以擺脫這些額外的行印刷。

有什么建議么? 我花了很長時間試圖解決它,但沒有成功。

附件是我的第二個宏代碼(請注意,與宏記錄器一樣長而混亂)。

Sub printSheetSend()
'
' printSheetSend Macro
'

'
Application.Goto Reference:="Print_Area"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = "$1:$1"
    .PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = "$B$1:$Y$567"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.7)
    .RightMargin = Application.InchesToPoints(0.7)
    .TopMargin = Application.InchesToPoints(0.75)
    .BottomMargin = Application.InchesToPoints(0.75)
    .HeaderMargin = Application.InchesToPoints(0.3)
    .FooterMargin = Application.InchesToPoints(0.3)
    .PrintHeadings = False
    .PrintGridlines = False
    .PrintComments = xlPrintNoComments
    .PrintQuality = 600
    .CenterHorizontally = False
    .CenterVertically = False
    .Orientation = xlLandscape
    .Draft = False
    .PaperSize = xlPaperA3
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .zoom = 100
    .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
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = "$1:$1"
    .PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = "$B$1:$Y$567"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.7)
    .RightMargin = Application.InchesToPoints(0.7)
    .TopMargin = Application.InchesToPoints(0.75)
    .BottomMargin = Application.InchesToPoints(0.75)
    .HeaderMargin = Application.InchesToPoints(0.3)
    .FooterMargin = Application.InchesToPoints(0.3)
    .PrintHeadings = False
    .PrintGridlines = False
    .PrintComments = xlPrintNoComments
    .PrintQuality = 600
    .CenterHorizontally = False
    .CenterVertically = False
    .Orientation = xlLandscape
    .Draft = False
    .PaperSize = xlPaperA3
    .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
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = "$1:$1"
    .PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = "$B$1:$Y$567"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.7)
    .RightMargin = Application.InchesToPoints(0.7)
    .TopMargin = Application.InchesToPoints(0.75)
    .BottomMargin = Application.InchesToPoints(0.75)
    .HeaderMargin = Application.InchesToPoints(0.3)
    .FooterMargin = Application.InchesToPoints(0.3)
    .PrintHeadings = False
    .PrintGridlines = False
    .PrintComments = xlPrintNoComments
    .PrintQuality = 600
    .CenterHorizontally = False
    .CenterVertically = False
    .Orientation = xlLandscape
    .Draft = False
    .PaperSize = xlPaperA3
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 0
    .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

' ADD: send shit to printer here
Columns("G:G").Select
Selection.EntireColumn.Hidden = False
Columns("I:I").Select
Selection.EntireColumn.Hidden = False
Columns("L:L").Select
Selection.EntireColumn.Hidden = False
Columns("O:O").Select
Selection.EntireColumn.Hidden = False
Columns("T:T").Select
Selection.EntireColumn.Hidden = False
Columns("U:U").Select
Selection.EntireColumn.Hidden = False
Columns("V:V").Select
Selection.EntireColumn.Hidden = False
Range("B1").Select
End Sub

因此,我正在編寫此代碼以打印列A,B和D,而不打印列C。

A___B___C___D
1   2   3   4
1   2   3   4
1   2   3   4


Dim ws as Worksheet
Dim TotalRows as Long
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = "Delete"
TotalRows = ws.Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("A" & Rows.Count).End(xlUp).Copy
ws.Range("A1").PasteSpecial
ActiveSheet.Range("B" & Rows.Count).End(xlUp).Copy
ws.Range("B1").PasteSpecial
ActiveSheet.Range("C" & Rows.Count).End(xlUp).Copy
ws.Range("C1").PasteSpecial
ws.PageSetup.PrintArea = ws.Range("A1:C" & TotalRows)
ws.PrintOut
Application.EnableEvents = False
ws.Delete
Application.EnableEvents = True

所以,如果一切順利,我將得到一份打印輸出,看起來像

A__B__D
1  2  4
1  2  4
1  2  4

要使用此代碼,您將不得不對其進行大量修改,但是我希望這可以成為一個框架。

對不起! 忽略這個問題!

原來有人在電子表格中添加了注釋,這些注釋也正在打印出來。代碼很好:)

暫無
暫無

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

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