我有一个宏来打印出活动工作表的内容。
我在VBA中编写的页面设置。
Sub Macro()
Application.ScreenUpdating = True
Worksheets("Reports").Columns("F").Hidden = True
Worksheets("Reports").Columns("R").Hidden = True
Worksheets("Reports").Range("H:J,M:P").EntireColumn.Hidden = True
Dim PrintThis As Range
Dim LastRow As Long
LastRow = Range("G" & Sheets("Reports").Rows.Count).End(xlUp).Row
Set PrintThis = Sheets("Reports").Range("F1" & ":T" & LastRow)
With Sheets("Reports").PageSetup
.Orientation = xlLandscape
.PrintTitleRows = "$1:$1"
.PrintArea = "$F:$T"
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.CenterHeader = "Report"
.CenterFooter = Format(Now(), "dd/mm/yy")
.RightFooter = "&P"
End With
PrintThis.PrintPreview
Worksheets("Reports").Columns("F").Hidden = False
Worksheets("Reports").Columns("R").Hidden = False
Worksheets("Reports").Range("H:J,M:P").EntireColumn.Hidden = False
Application.ScreenUpdating = False
End Sub
代码缩小内容,使其适合横向页面宽度的一半。我没有设置FitToPageTall,因为内容的长度可能会根据工作表中的其他执行情况而改变。我使用第一行作为打印标题。我在这个过程中隐藏了几个列,因为它们在报告中不需要,但它们的宽度仍然占据了页面上的空间。
答案 0 :(得分:1)
根据我之前的所有评论,代码应如下所示:
Sub Macro()
Application.ScreenUpdating = True
Worksheets("Reports").Columns("F").Hidden = True
Worksheets("Reports").Columns("R").Hidden = True
Worksheets("Reports").Range("H:J,M:P").EntireColumn.Hidden = True
Dim PrintThis As Range
Dim LastRow As Long
LastRow = Range("G" & Sheets("Reports").Rows.Count).End(xlUp).Row
Set PrintThis = Sheets("Reports").Range("F1" & ":T" & LastRow)
With Sheets("Reports").PageSetup
.Orientation = xlLandscape
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
.PrintArea = "$F:$T"
.FitToPagesWide = 1
.FitToPagesTall = False
.Zoom = False
.CenterHeader = "Report"
.CenterFooter = Format(Now(), "dd/mm/yy")
.RightFooter = "&P"
End With
PrintThis.PrintPreview
Worksheets("Reports").Columns("F").Hidden = False
Worksheets("Reports").Columns("R").Hidden = False
Worksheets("Reports").Range("H:J,M:P").EntireColumn.Hidden = False
Application.ScreenUpdating = False
End Sub
这是为我测试并在2010年工作。
答案 1 :(得分:0)
在VBA中将printarea设置为范围名称并打印它通常是个好主意。如果工作表发生更改,则相应地更改printarea。因此,想法是正确设置[input_print_area]。通常我会使用这样的东西:
Public Sub PrintPage()
Dim Sh As Worksheet
Dim rngPrint As Range
On Error GoTo PrintPage_Error
s_reduce_paper_title = "Go Green!"
ActiveSheet.PageSetup.BlackAndWhite = Not tbl_Input.cb_print_color
Set Sh = ActiveSheet
Set rngPrint = [input_print_area]
With Sh.PageSetup
.Orientation = xlPortrait
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With
Select Case MsgBox("Are you sure that you want to print?", vbYesNo Or vbQuestion Or vbDefaultButton1, s_reduce_paper_title)
Case vbYes
Select Case MsgBox("Really sure that you want to print?", vbYesNo Or vbQuestion Or vbDefaultButton1, s_reduce_paper_title)
Case vbYes
rngPrint.PrintOut
End Select
End Select
On Error GoTo 0
Exit Sub
PrintPage_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure PrintPage of Modul mod_Drucken"
End Sub