如何将工作表列表传递给VB Sheets.Select

时间:2015-06-21 02:48:00

标签: excel-vba excel-2013 vba excel

我试图在工作簿中选择特定工作表。我有一个公式来计算需要打印的特定表格,然后创建一个带引号的逗号分隔列表,即"Page 1", "Page 5", "Page 18"

现在我尝试将其传递给select,使用以下代码将其导出为PDF:

Sub Print_PDF()

Dim FName As String
Dim Print_Sheets As String

FName = Worksheets("ControlSheet").Cells(5, "B").Value
Print_Sheets = Worksheets("ControlSheet").Cells(56, "G").Value

Sheets(Array(Print_Sheets)).Select
Sheets("Page 1").Activate

Save_Out = "C:\Temp\" + FName + ".pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Save_Out, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True
Sheets("Page 1").Select
End Sub

但是,Print_Sheets会传递一组额外的引号,即""Page 1", "Page 5", "Page 18""

如何删除多余的引号或是否有更好的方法将表单列表传递给Sheets.Select?

1 个答案:

答案 0 :(得分:1)

在物理删除双引号之后,最好将工作表名称移动到预定义的字符串数组:

'Define a string array for the list of worksheets
Dim arrSheets() As String

...
...

Print_Sheets = Worksheets("ControlSheet").Cells(16, "G").Value

'Remove the double quotes from the delimited list of worksheets
Print_Sheets = Replace(Print_Sheets, """", "")

'Break the worksheet names out into a string array
arrSheets =  Split(Print_Sheets, ",")

'Select the worksheets using the array
Sheets(arrSheets).Select

...
...