Run Time Error 13: Type Mismatch

 ̄綄美尐妖づ 提交于 2019-12-02 07:29:17

问题


I am having a problem with the following code and am getting a type mismatch error on the bolded line of code:

 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

回答1:


You cannot create/pass the array like this. Try this (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

ONE MORE WAY

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



回答2:


As said here your code is working.

You get Type Mismatch error in the modified code because Sheets variable is declared as variant. Simply remove it and your code will work again.



来源:https://stackoverflow.com/questions/19643307/run-time-error-13-type-mismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!