2013-10-28 94 views
1

我有下面的代碼的問題,我對代碼的粗體線得到一個類型不匹配錯誤:運行時錯誤13:類型不匹配

Private Sub CommandButton3_Click() 
    Application.ScreenUpdating = False 
    Dim p 
    Dim ActivePrinter 
    Dim Sheets 

    p = Application.ActivePrinter 
    ActivePrinter = ("Send to OneNote 2010") 

    **Sheets(Array("R-Overview", "R-Savings", "R-Table")).PrintOut , , 1** 

    End Sub 

回答

2

您不能創建/通過這樣的陣列。試試這個(久經考驗

Private Sub CommandButton3_Click() 
    Application.ScreenUpdating = False 

    Dim p 
    Dim ActivePrinter 
    Dim shtsArray(1 To 3) As String 

    p = Application.ActivePrinter 
    ActivePrinter = ("Send to OneNote 2010") 

    shtsArray(1) = "R-Overview" 
    shtsArray(2) = "R-Savings" 
    shtsArray(3) = "R-Table" 

    Sheets(shtsArray).PrintOut , , 1 

    Application.ScreenUpdating = True 
End Sub 

另一種方式

Private Sub CommandButton3_Click() 
    Application.ScreenUpdating = False 

    Dim p 
    Dim ActivePrinter 
    Dim shtsArray 
    Dim sheetNames As String 

    p = Application.ActivePrinter 
    ActivePrinter = ("Send to OneNote 2010") 

    sheetNames = "R-Overview,R-Savings,R-Table" 
    shtsArray = Split(sheetNames, ",") 

    Sheets(shtsArray).PrintOut , , 1 
End Sub 
+0

我在倒數第二行得到「運行時錯誤'1004':應用程序定義錯誤或對象定義錯誤」。 –

+0

我只是測試它,它的工作原理。 –

+0

嘗試我剛剛更新的第二種方法。 –

0

至於說here您的代碼工作。

由於Sheets變量被聲明爲variant,所以在修改的代碼中出現Type Mismatch錯誤。只需將其刪除,您的代碼將再次運行。

相關問題