/ / Subscrito fora do intervalo, o que estou perdendo? - excel, vba, intervalo

Subscrito fora do intervalo, o que estou perdendo? - excel, vba, intervalo

Eu tenho uma macro do Excel na qual estou tentando executartodas as folhas da pasta de trabalho. É apenas para configurar as áreas de impressão e as quebras de página, mas existem 460 folhas para fazer. Elas estão exatamente no mesmo formato, por isso deve ser simples. Estou usando os codinomes da planilha para que não seja um questão.

A macro funciona na planilha ativa e o erro ocorre quando tenta fazer um loop para a próxima planilha.

Folhas ("planilha" + LTrim (Str (i + 1)) + "").Select é a linha na qual está depurando. Por favor, veja o código inteiro abaixo. Sinto que essa é uma madeira bastante simples para o momento das árvores, portanto qualquer ajuda seria muito apreciada!

    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

Respostas:

0 para resposta № 1

Cada planilha é um objeto na coleção de planilhas - e você pode percorrer cada item de uma coleção usando um For Each loop.

Seu código original parece ter sido gravado eatualizado (nada de errado nisso). O gravador de macro registra tudo - inclusive as configurações padrão (para que você não precise especificar seu valor) somente se desejar que ele seja diferente do padrão). Linhas como ActiveWindow.SmallScroll Down:=24 pode ser removido, pois basta clicar com o mouse para rolar a tela um pouco.

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 para resposta № 2

Encomende suas planilhas do Excel por posição e você pode usar:

Sheets(i + 1).Select

ao invés de :

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

As folhas de exemplo (1) podem ser as primeiras na posição. Folhas (2) é a segunda, e assim por diante ... Todas as suas folhas serão marcadas por suas posições. Mas tome cuidado para não criar / excluir planilhas. Espero que isso possa ajudar!