VBA宏打印循环

时间:2017-09-14 01:22:10

标签: excel vba excel-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 

希望能让人有点头疼。

1 个答案:

答案 0 :(得分:3)

首先,让我更正一下你的代码:

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表示&#34; FitToPagesWide和FitToPagesTall属性控制工作表的缩放方式。&#34; 。< / p>

在您的代码中,您在这2个属性之前使用Zoom,因此您将覆盖。

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

.FitToPagesWide = 1
.FitToPagesTall = False