2017-09-14 157 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