VBAmacros打印循环

[下面更新]

我一直在为我的制作表编写一个打印macros。

除了实际的打印输出外,其他的都很好。 如果我使用.Zoom = False而不是.Zoom = 50,则打印输出表上的printarea会变得很小。 如果我使用缩放比例= 50,那么左右两边就会有这么大的空白。 我怀疑是不是处理实际的printarea行,但我不知道为什么,因为其他命令行似乎工作得很好。 我试图剥离代码几乎printarea,fitTopagesxx,并得到了同样的问题。

我试着多次重写代码,并得到一个错误提示或与在网上find的其他代码相同的结果。

Sub PrintJob() Dim ws As Worksheet Dim i As Long Set ws = Sheets("Filtered_List") For i = 2 To ws.Cells(Rows.Count, "F").End(xlUp).Row If ws.Cells(i, "F").Value = 0 Then Exit For With Sheets("Print_Page") .Range("C8").Value = ws.Cells(i, "F").Value Worksheets("Print_Page").PageSetup.PrintArea = "$C$2:$L$60" Worksheets("Print_Page").PageSetup.Orientation = xlPortrait Worksheets("Print_Page").PageSetup.Zoom = 50 Worksheets("Print_Page").PageSetup.FitToPagesWide = 1 Worksheets("Print_Page").PageSetup.FitToPagesTall = False Worksheets("Print_Page").PageSetup.LeftMargin = Application.InchesToPoints(0) Worksheets("Print_Page").PageSetup.RightMargin = Application.InchesToPoints(0) Worksheets("Print_Page").PageSetup.TopMargin = Application.InchesToPoints(0) Worksheets("Print_Page").PageSetup.BottomMargin = Application.InchesToPoints(0) Worksheets("Print_Page").PageSetup.HeaderMargin = Application.InchesToPoints(0) Worksheets("Print_Page").PageSetup.FooterMargin = Application.InchesToPoints(0) .PrintOut End With Next i End Sub 

[更新:]我find了一些帮助后,发现这是一个表特定的错误后的问题。 基本上,打印标题字段需要是空的,那是这样的代码:

 .PrintTitleRows = "" .PrintTitleColumns = "" 

我添加了几行,使用Noldor130884的清理代码:

 Sub PrintJob() Dim ws As Worksheet Dim i As Long Set ws = Sheets("Filtered_List") For i = 2 To ws.Cells(Rows.Count, "F").End(xlUp).Row If ws.Cells(i, "F").Value = 0 Then Exit For With Worksheets("Print_Page") .Range("C8").Value = ws.Cells(i, "F").Value With .PageSetup .PrintArea = "$C$2:$L$60" .Orientation = xlPortrait .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = False .LeftMargin = Application.InchesToPoints(0) .RightMargin = Application.InchesToPoints(0) .TopMargin = Application.InchesToPoints(0) .BottomMargin = Application.InchesToPoints(0) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) .PrintTitleRows = "" .PrintTitleColumns = "" .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0) .RightMargin = Application.InchesToPoints(0) .TopMargin = Application.InchesToPoints(0) .BottomMargin = Application.InchesToPoints(0) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) .PrintHeadings = False .CenterHorizontally = True .CenterVertically = False .PaperSize = xlPaperLetter End With .PrintPreview End With Next i End Sub 

希望能省一点头痛。

首先,让我修改一下你的代码:

 With Worksheets("Print_Page") .Range("C8").Value = ws.Cells(i, "F").Value With .PageSetup .PrintArea = "$C$2:$L$60" .Orientation = xlPortrait .Zoom = 50 .FitToPagesWide = 1 .FitToPagesTall = False .LeftMargin = Application.InchesToPoints(0) .RightMargin = Application.InchesToPoints(0) .TopMargin = Application.InchesToPoints(0) .BottomMargin = Application.InchesToPoints(0) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) End With .PrintOut End With 

现在,请注意, 正如Microsoft所述 , Zoom = False意味着“FitToPagesWide和FitToPagesTall属性控制如何缩放工作表”。

在你的代码中,你在这两个属性之前使用Zoom ,所以你正在覆盖。

如果我正确理解你想要做什么,请从你的代码中删除:

 .FitToPagesWide = 1 .FitToPagesTall = False