【问题标题】:VBA print macro to set page in specific mannerVBA打印宏以特定方式设置页面
【发布时间】:2016-09-16 20:55:45
【问题描述】:

我一直在研究一个我认为可能很简单的打印宏。我已经尝试录制一个宏,并且已经研究了几个小时来查看其他人的代码。我想要的是宏:

1) 选择活动工作表中的所有单元格

2) 设置打印比例以使所有列适合一页

3) 打印横向模式

4) 打开打印预览(如果可能)

5) 如果#4 不可行,则执行打印作业。

当我运行我当前的代码时,我的 Excel 工作表被分成大量的页面(棋盘样式),然后我得到一个错误代码。谢谢阅读。

这是我当前的代码:

Sub PrintNOPAsheet()'
' PrintNOPAsheet Macro



 Cells.Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = ""
    .PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = "$A$1:$H$346"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .LeftHeader = ""
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.25)
    .RightMargin = Application.InchesToPoints(0.25)
    .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 = xlPaperLetter
    .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

Selection.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
End Sub

'

【问题讨论】:

    标签: vba excel printing


    【解决方案1】:

    这是我通常使用的,然后我做了它来匹配你的问题。在With 中,您可以添加尽可能多的来自录制宏的属性以适合您的代码。

    Sub printIt()
    
    Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
    Dim rng as Range
    Dim printRange as String
    
    Set rng = ws.Range("A1:J11")
    
    ''''For Dynamic Ranges'''''
    With ws
        Set rng = .Range(.Range("A1"),.Range("J11").End(xlDown))
    End With
    
    ''''Range from User Highlighted Cells''''
    Set rng = Selection
    ''''This method is not the best way''''
    
    printRange = ws.Name & "!" & rng.Address
    
    With ws.PageSetup
        .PrintArea = printRange
        .Zoom = False
        .FitToPagesWide = 1 'Question 2
        .Orientation = xlLandscape 'Question 3
    End With
    
    ws.PrintOut preview:=True 'Question 4
    
    End Sub
    

    【讨论】:

    • 糟糕,我把那部分遗漏了。我还希望它打印选定的单元格。
    • 我也想说声谢谢!除了我仍然需要打印选定的单元格之外,您编写的代码运行良好。
    • 添加到代码中以解决您的问题。希望这会有所帮助!
    • 如何使范围动态化?
    • 类似 ws.Range(ws.Range("A1"), ws.Range("D1").End(xlDown)) 的东西。这将使 Range 成为垂直列并向下延伸到最后一个值。此代码使从 A 列到 D 列的框,然后向下到最后一行值的范围。要尝试一下,请将“1”添加到 A 到 D 列中的单元格,然后向下添加到您想要的任何行。然后在一个模块中,添加此代码加上“.Select”以查看 excel 如何获取该范围。确保设置工作表变量。 (抱歉冗长的回复)
    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多