下标超出范围,我缺少什么?

时间:2016-11-18 09:52:21

标签: excel vba range

我有一个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

2 个答案:

答案 0 :(得分:0)

按位置订购Excel工作表,您可以使用:

Sheets(i + 1).Select

而不是:

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

示例表格(1)可能是第一个就位。表格(2)是第二个,依此类推......所有的表格都会被他们的位置标记。但要注意不要创建/删除工作表。 希望这可以提供帮助!

答案 1 :(得分: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