Arrays 选择数组I';时下标超出范围;我创造了

Arrays 选择数组I';时下标超出范围;我创造了,arrays,excel,vba,pdf,Arrays,Excel,Vba,Pdf,我试图创建一个单独的PDF文件,其中包含我在控制表中从单元格J2列出的每个选项卡的一张表,但我不断得到一个下标超出范围的错误 当我记录动作时,我看到它创建了一个工作表名称数组,然后选择导出,因此我有一个For循环,它遍历列表并创建一个数组,该数组将自身添加到列表的末尾,目的是创建一个长字符串,然后我选择该字符串作为数组 一切看起来都很好(变量PDFArray以正确的格式显示了一个选项卡名称字符串),但当我到达“工作表(数组(PDFArray))”行时,选择“然后我得到错误”。我已确保工作表名称不

我试图创建一个单独的PDF文件,其中包含我在控制表中从单元格J2列出的每个选项卡的一张表,但我不断得到一个下标超出范围的错误

当我记录动作时,我看到它创建了一个工作表名称数组,然后选择导出,因此我有一个For循环,它遍历列表并创建一个数组,该数组将自身添加到列表的末尾,目的是创建一个长字符串,然后我选择该字符串作为数组

一切看起来都很好(变量PDFArray以正确的格式显示了一个选项卡名称字符串),但当我到达“工作表(数组(PDFArray))”行时,选择“然后我得到错误”。我已确保工作表名称不包含不需要的字符或空格,但仍然没有欢乐。任何帮助都将不胜感激。多谢各位

    Sub B_PDFs()

    Dim PDFarray As String, PDFName as String, sht As String

    Sheets("Control").Select

    PLFile = ActiveWorkbook.Name
    PDFLoc = Application.ActiveWorkbook.Path & "\"
    PDFName = Range("A20")
    PDFSheetCount = Range("J1").Offset(Rows.Count - 1, 0).End(xlUp).Row

    'Loop through column J and create a string with each tab name to be exported
        For x = 2 To PDFSheetCount Step 1

            If x = PDFSheetCount Then   
                sht = """ " & "" & Cells(x, 10) & """ "
            Else
                sht = """" & "" & Cells(x, 10) & """" & ", "
            End If
            PDFarray = PDFarray & sht

        Next x

    'Create PDF from the array above
        Worksheets(Array(PDFarray)).Select   - this is where I get the error Subscript Out Of Range
        Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PFDLoc & PDFName, Quality:= _
            xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, 
    OpenAfterPublish:=False

    Workbooks(PLFile).Activate

    End Sub

@RonRosenfeld所建议的关于
select
selection
的内容是正确的。您正在构建的表达式是字符串,而Excel希望它是实数组

因此,原则上,下面这样的方法将适用于您,它将创建一个用于处理的阵列,并可根据您的需要使用

Dim shtNames As Variant
Dim pdfArray
shtNames = Range("J2:J" & Range("J1").Offset(Rows.Count - 1, 0).End(xlUp).Row).Value
pdfArray = Application.Transpose(shtNames)

我不明白为什么MS将不需要变量声明作为默认值。选择
Tools/Options/Editor
并选中
Require Variable Declaration
。这将在任何新模块的开始处放置
选项Explicit
。要更正此模块,请在开头手动输入

这样做将使您能够发现并纠正代码中的拼写错误

您还应该避免
选择
选择
激活
。它们很少有任何用途,并且可能会导致多个问题,因为它们会使您避免明确声明您需要的工作簿、工作表等。看

但是在使用
ExportAsFixedFormat
方法导出所选工作表时,似乎需要
Selection
ActiveSheet
才能工作

Array(str\u变量)
返回一个包含整个字符串变量的单个条目的数组。它不会解释字符串变量以便将其拆分为单独的元素

因此,稍微重写您的代码(我将留给您清理PDF文档):


循环后是否尝试过debug.print PDFarray?您得到的值是多少?
Array(PDFArray)
将返回一个数组,其中包含单个元素,其中包含整个
PDFArray
字符串。您还应该将
选项Explicit
放在模块的开头,并显式声明所有变量。而且您的任何代码中都不需要
Select
Selection
。明确声明并使用工作表变量。非常感谢您的回答和反馈。PDF现在按照我的希望创建,我将采纳您关于最佳实践的建议,并将跟进您建议的文章。@SteveJ很乐意提供帮助。如果我的回答满足您的问题,如果您能将其标记为已接受,我将不胜感激。你可以阅读更多信息。
Option Explicit
Sub B_PDFs()

Dim PDFarray As Variant, PDFName As String, PLFile As String, PDFLoc As String
Dim wsControl As Worksheet
Dim WB As Workbook

'Consider wheter you want to use ThisWorkbook or a specific workbook
Set WB = ThisWorkbook
With WB
    Set wsControl = .Worksheets("Control")
    PLFile = .Name
    PDFLoc = .Path & "\"
End With

With wsControl
    PDFName = .Range("A20")

    'create PDFarray
    'This will be a 1-based 2D array starting at J1
    'If you need to start at J2, alter the initial cell
    PDFarray = .Range(.Cells(1, 10), .Cells(.Rows.Count, 10).End(xlUp))
End With

'convert to a 1D array
PDFarray = WorksheetFunction.Transpose(PDFarray)


'Note the use of `Select` and `ActiveSheet` when using this `ExportAsFixedFormat` method
Worksheets(PDFarray).Select
'Create PDF from the array above

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFLoc & PDFName, Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False


End Sub