如何将工作表列表传递给 VB Sheets.Select
How to pass a list of sheets to a VB Sheets.Select
我正在尝试 select 工作簿中的特定工作表。我有一个公式可以计算需要打印的特定工作表,然后创建一个用引号分隔的列表,即 "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?
您最好在物理删除双引号后将工作表名称移动到预定义的字符串数组,如下所示:
'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
...
...
我正在尝试 select 工作簿中的特定工作表。我有一个公式可以计算需要打印的特定工作表,然后创建一个用引号分隔的列表,即 "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?
您最好在物理删除双引号后将工作表名称移动到预定义的字符串数组,如下所示:
'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
...
...