我有一个Excel宏,我试图在工作簿中的每张工作表上运行。这只是设置打印区域和分页符,但有460张纸。它们都是完全一样的格式,所以它应该是直截了当的。我正在使用工作表代号,所以不应该成为问题。下标超出范围,我错过了什么?
该宏在活动工作表上工作,然后当它尝试循环到下一个工作表时出现错误。
表( “片” + LTRIM(STR(I + 1))+ “”)。选择是它的调试就行了。请参阅下面的整个代码。我有一种感觉,这是一个非常简单的树木时刻,所以任何帮助将不胜感激!
Sub setup()
Dim i As Long
For i = 1 To 460
ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
ActiveWindow.SmallScroll Down:=24
Set ActiveSheet.HPageBreaks(1).Location = Range("A64")
ActiveWindow.SmallScroll Down:=-66
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$3"
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&A"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.236220472440945)
.RightMargin = Application.InchesToPoints(0.236220472440945)
.TopMargin = Application.InchesToPoints(0.748031496062992)
.BottomMargin = Application.InchesToPoints(0.748031496062992)
.HeaderMargin = Application.InchesToPoints(0.31496062992126)
.FooterMargin = Application.InchesToPoints(0.31496062992126)
.PrintHeadings = False
.PrintGridlines = True
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 46
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = False
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
Sheets("sheet" + LTrim(Str(i + 1)) + "").Select
Next i
End Sub
尝试替换。使用.Activate选择。 – DoctorMick
“我正在使用工作表代号”:不,您不知道。在“表格”(“Sheet1”)中,“Sheet1”的名称不是代号,而是表格的真实名称。 –
为什么不使用'ThisWorkBook.Worksheets中的每一个wkSht'(使用'Dim wrkSht as WorkSheet')...'NextWrkSht'。您需要用'wrkSht'替换'ActiveWorksheet'的每个实例。 –