我遇到运行时错误'9': ActiveWorkbook.Sheets(myArr())。Select 行上的下标超出范围。请帮忙。
Private Sub CommandButton1_Click()
Dim sH As Worksheet
Dim Assy As String
Dim Path As String
Dim ShName As String
Dim myArr() As Variant
Dim X As Integer
If CheckBox1.Value = True Then Worksheets("Supplier PDFs").Activate 'make Supplier PDFs sheet active sheet
Assy = Range("C5").Text 'Assembly name
Path = Range("H5").Text 'Directory path name
If Assy = "" Or Path = "" Then 'If name or folder path is empty,
MsgBox "An entry was left blank! Please provide name and/or filepath." 'Give error message
End
Else
End If
For Each sH In ActiveWorkbook.Worksheets 'for each worksheet
ReDim myArr(X) 'redeclare matrix myArr with size X
sH.Activate 'activate sheet
ShName = ActiveSheet.Name 'ShName is the name of the active sheet
ActiveSheet.PageSetup.FitToPagesWide = 1 'Set page width to 1-page
ActiveSheet.PageSetup.Orientation = xlLandscape 'Set orientation to landscape
ActiveSheet.PageSetup.Draft = False 'Print with graphics
ActiveSheet.PageSetup.PaperSize = xlPaperLetter 'Set paper size to letter
If Range("F3") = Assy And ActiveSheet.Visible = True Then 'if assembly level is right,
myArr(X) = ShName 'update array element with sheet name
X = X + 1 'index counter X
Else 'if not, do nothing
End If
Next sH 'end for loop
ReDim Preserve myArr(X) 'reset myArr array size to 0
**ActiveWorkbook.Sheets(myArr()).Select** 'select all sheets with sheetnames in myArr
ActiveSheet.PageSetup.PrintArea = "$A$1:$R$26" 'Set print area to sheet limits
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Path & Assy & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True 'Set filename, quality, properties and open after publish
'reset myArr size to zero
If X > 0 Then
X = X - 1
Else
End If
ReDim myArr(0)
End If
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub