1

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

 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
4

2 回答 2

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
于 2013-10-28T19:24:17.373 回答
0

如此处所述您的代码正在运行。

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

于 2013-10-29T05:47:03.363 回答