excel VBA,试图打印一个区域?

时间:2015-04-20 20:55:04

标签: vba printing

我现在正在制作一本大工作簿,试图在每个月末我都运行一个生成新工作表的宏。

我现在已经整理好了。

接下来是一个宏,它为用户提供了从工作表中仅打印相关信息的选项。

这有点复杂!由于数据中间的某些列不应打印,但我仍然希望它们在其他所有时间都可见。

我的解决方案:

(注意我对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

2 个答案:

答案 0 :(得分:0)

所以我编写此代码来打印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

要使用此代码,您必须对其进行相当大的修改,但我希望这可以成为一个框架。

答案 1 :(得分:0)

抱歉全部!忽略这个问题!

原来有人在电子表格中添加了评论,这些也是打印出来的..代码很好:))