[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
希望这节省了一些头痛的问题。
我会检查通过对页面设置对话框中的值,以确保没有什么奇怪例如回事在页眉和页脚。 – Joffan
如果我通过使用ui直接从excel甚至宏/ vba区域打印其他东西,它看起来非常好。相当令人沮丧。 – FatTwin
真的很奇怪。我不认为添加IgnorePrintAreas:= False会有帮助吗?...另外我注意到您不使用With对象来设置PageSetup值,也许尝试使用相同的方式.PrintOut?也就是说,有Worksheets(“Print_Page”)。PrintOut – Joffan