运行时错误13:类型不匹配

时间:2013-10-28 19:20:46

标签: excel excel-vba runtime-error vba

我遇到以下代码的问题,并且在粗体代码行中出现类型不匹配错误:

 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 个答案:

答案 0 :(得分:2)

您无法像这样创建/传递数组。试试这个( TRIED AND TESTED

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

答案 1 :(得分:0)

如上所述here您的代码正在运行。

在修改后的代码中出现类型不匹配错误,因为Sheets变量被声明为variant。只需删除它,您的代码将再次运行。