2016-11-18 61 views
0

我有一個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 
+1

嘗試替換。使用.Activate選擇。 – DoctorMick

+1

「我正在使用工作表代號」:不,您不知道。在「表格」(「Sheet1」)中,「Sheet1」的名稱不是代號,而是表格的真實名稱。 –

+0

爲什麼不使用'ThisWorkBook.Worksheets中的每一個wkSht'(使用'Dim wrkSht as WorkSheet')...'NextWrkSht'。您需要用'wrkSht'替換'ActiveWorksheet'的每個實例。 –

回答

0

每個工作表是在工作表集合中的對象 - 你可以在集合中使用For Each遍歷每個項目的步驟。

您的原始代碼看起來像被記錄和更新(沒有錯誤)。宏記錄器記錄了一切 - 包括默認設置的設置(所以你不需要指定它們的值 - 只有當你希望它不同於默認設置時)。 可以刪除諸如ActiveWindow.SmallScroll Down:=24之類的行,因爲只需單擊鼠標即可向下滾動屏幕。

Public Sub SetUp() 

    Dim wrkSht As Worksheet 

    Application.PrintCommunication = False 

    'Look at each wrksht in turn. 
    'If you want to ignore certain worksheets you could use a 
    'SELECT CASE statement within the loop. 
    For Each wrkSht In ThisWorkbook.Worksheets 
     With wrkSht 
      'Remove all manually added page breaks. 
      'Resets to automatic page breaks. 
      'Microsoft Help helpfully just says: 
      'Resets all page breaks on the specified worksheet (thanks for that MS). 
      .ResetAllPageBreaks 
      .HPageBreaks.Add Before:=.Range("A64") 
      With .PageSetup 
       'You can ignore settings that are set as default. 
       'Not sure what they all are, but probably include anything that ends in ="" 
       'for a start. 
       .PrintTitleRows = "$1:$3" 
       .CenterHeader = "&A" 
       .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 
       .PrintComments = xlPrintNoComments 
       .PrintQuality = 600 
       .Orientation = xlLandscape 
       .Zoom = 46 
      End With 
     End With 
    Next wrkSht 

    Application.PrintCommunication = True 

End Sub 
+0

非常感謝修復的代碼以及宏記錄中的額外信息。它完美的作品。 – scrapcode

0

訂單您的Excel表由位置和可以使用:

Sheets(i + 1).Select 

代替:

Sheets ("sheet" + LTrim (Str (i + 1)) + "").Select 

實施例表(1)可以是在位置的第一。表單(2)是第二個,依此類推......所有表單都將以其位置標記。但要小心不要創建/刪除工作表。 希望這可以幫助!