2016-11-18 77 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)是第二个,依此类推......所有表单都将以其位置标记。但要小心不要创建/删除工作表。 希望这可以帮助!