2015-06-21 23 views
0

我試圖在工作簿中選擇特定的工作表。我有一個公式,計算需要打印的特定工作表,然後創建一個帶引號的逗號分隔列表,即"Page 1", "Page 5", "Page 18"如何將工作表的列表傳遞給VB工作表。選擇

現在我試圖將它傳遞給選擇使它導出到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

你最好的移動工作表名稱預先定義的字符串數組後,你物理刪除雙引號是這樣的:

'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 

... 
... 
+0

這產生於表錯誤(arrSheets)。選擇線 – Reeza

+0

工程確定爲了我。具體的錯誤是什麼?你有沒有在該指令上加一個斷點來檢查'arrSheets'的內容?你確定所有這些工作表都存在嗎? – joehanna

+0

表格是逗號和空格分隔',',所以上面的方法是在表格名稱中保留空格,所以雖然看起來正確,但它們不存在。 – Reeza

相關問題