2017-09-14 159 views
2

[update below]VBA宏打印循环

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

除了实际的打印输出外,其他所有的东西都很棒。如果我使用.Zoom = False而不是.Zoom = 50,则打印输出表上的printarea会变得很小。如果我使用zoom = 50,我可以在左侧和右侧获得这些英寸宽的边距。我怀疑它不会处理实际的printarea行,但我不知道为什么其他命令行似乎工作得很好。我试图将代码剥离到非常多的printarea,fitTopagesxx,并得到同样的问题。

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

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 

[更新]我发现了它是一个特定纸张错误后想通了这个问题一些帮助后在这里。基本上,打印标题字段需要是空的,这确实是该代码是这一个:

.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 

希望这节省了一些头痛的问题。

+0

我会检查通过对页面设置对话框中的值,以确保没有什么奇怪例如回事在页眉和页脚。 – Joffan

+0

如果我通过使用ui直接从excel甚至宏/ vba区域打印其他东西,它看起来非常好。相当令人沮丧。 – FatTwin

+0

真的很奇怪。我不认为添加IgnorePrintAreas:= False会有帮助吗?...另外我注意到您不使用With对象来设置PageSetup值,也许尝试使用相同的方式.PrintOut?也就是说,有Worksheets(“Print_Page”)。PrintOut – Joffan

回答

2

首先,请允许我纠正你的代码位:

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 

现在,请注意as Microsoft saysZoom = False意味着“的FitToPagesWide和FitToPagesTall属性控制工作是如何进行缩放。”

在你的代码中,你在这两个属性之前使用了一个Zoom,因此你正在覆盖。

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

.FitToPagesWide = 1 
.FitToPagesTall = False 
+0

我对Zoom部分很熟悉,但都没有设置工作。我编辑了我的原始帖子,以反映所需的更改。我用你的清理代码,谢谢你。 – FatTwin