Excel 选择单元格范围,然后打印
我有以下代码,可以正常工作:Excel 选择单元格范围,然后打印,excel,vba,Excel,Vba,我有以下代码,可以正常工作: Sheets("File1").Select Range("B2:J56").Select Selection.PrintOut Copies:=1, Collate:=True 但我需要打印几个范围,页面应符合以下格式: 每种选择都应适合A4纸 双面的 在一个页面中调整列,使内容完全适合页面宽度 600 dpi的高质量打印 居中的边距(水平和垂直) 页面应包括标题“此数据是一年前收集的” 在互联网上苦苦挣扎之后,我已经录制了一个宏,但是当我试图重现代码片段时
Sheets("File1").Select
Range("B2:J56").Select
Selection.PrintOut Copies:=1, Collate:=True
但我需要打印几个范围,页面应符合以下格式:
- 每种选择都应适合A4纸
- 双面的
- 在一个页面中调整列,使内容完全适合页面宽度
- 600 dpi的高质量打印
- 居中的边距(水平和垂直)
- 页面应包括标题“此数据是一年前收集的”
ActiveSheet.PageSetup.PrintArea=“$B$2:$J$56;$L$2:$T$37;$V$2:$AH$37”
和打印通信方法失败
[编辑]:解决方案如下所示
Sub RECORDED_Macro_Print_test()
Application.ScreenUpdating = False
Sheets("File1").Select
Range("B2:J83,L2:T37,V2:AO92").Select
Range("V2").Activate
ActiveSheet.PageSetup.PrintArea = "$B$2:$J$83;$L$2:$T$37;$V$2:$AO$92" ' <----- Fix it by replacing ';' by ',' as suggested by the user 'Siva'
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = "" ' <----- Not necessary
.PrintTitleColumns = "" ' <----- Not necessary
.LeftHeader = "" ' <----- Not necessary
.CenterHeader = "This data was collected a year ago"
.RightHeader = "" ' <----- Not necessary
.LeftFooter = "" ' <----- Not necessary
.CenterFooter = "" ' <----- Not necessary
.RightFooter = "" ' <----- Not necessary
.LeftMargin = Application.InchesToPoints(0.511811023622047)
.RightMargin = Application.InchesToPoints(0.511811023622047)
.TopMargin = Application.InchesToPoints(0.78740157480315)
.BottomMargin = Application.InchesToPoints(0.78740157480315)
.HeaderMargin = Application.InchesToPoints(0.31496062992126)
.FooterMargin = Application.InchesToPoints(0.31496062992126)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = True
.CenterVertically = True
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = "" ' <----- Not necessary
.EvenPage.CenterHeader.Text = "" ' <----- Not necessary
.EvenPage.RightHeader.Text = "" ' <----- Not necessary
.EvenPage.LeftFooter.Text = "" ' <----- Not necessary
.EvenPage.CenterFooter.Text = "" ' <----- Not necessary
.EvenPage.RightFooter.Text = "" ' <----- Not necessary
.FirstPage.LeftHeader.Text = "" ' <----- Not necessary
.FirstPage.CenterHeader.Text = "" ' <----- Not necessary
.FirstPage.RightHeader.Text = "" ' <----- Not necessary
.FirstPage.LeftFooter.Text = "" ' <----- Not necessary
.FirstPage.CenterFooter.Text = "" ' <----- Not necessary
.FirstPage.RightFooter.Text = "" ' <----- Not necessary
End With
Application.PrintCommunication = True ' <----- Remove this line to fix it. This is an error issue
Selection.PrintOut Copies:=1, Collate:=True
End Sub
Sub-RECORDED\u Macro\u Print\u test()
Application.ScreenUpdating=False
工作表(“文件1”)。选择
范围(“B2:J83,L2:T37,V2:AO92”)。选择
范围(“V2”)。激活
ActiveSheet.PageSetup.PrintArea=“$B$2:$J$83;$L$2:$T$37;$V$2:$AO$92”替换代码行
ActiveSheet.PageSetup.PrintArea = "$B$2:$J$56;$L$2:$T$37;$V$2:$AH$37"
与
我已经在我的机器上试过了,更换后该行没有出现任何错误OK。四个小时以上的时间来解决这个问题,我得到了-1分?如果答案似乎很简单,请告知解决方案。宏记录保存了实际状态的记录,不仅将其作为最终代码片段的一部分,还增加了您不再需要的信息,例如:以前的质量打印最初设置为72 dpi,我必须手动擦除几行。在修复分号问题并删除.PrintCommunication=True
之后,它起到了作用。多谢各位。
ActiveSheet.PageSetup.PrintArea = "$B$2:$J$56,$L$2:$T$37,$V$2:$AH$37"