我有一個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'的每個實例。 –