使用ActiveWorkbook时Excel VBA类型不匹配13。关闭

使用ActiveWorkbook时Excel VBA类型不匹配13。关闭,excel,vba,type-mismatch,Excel,Vba,Type Mismatch,我在打开文件的主工作簿中运行代码;执行数据刷新;然后我想保存文件。在我尝试使用ActiveWorkbook.Close True保存工作簿之前,一切正常。我得到一个“运行时错误13类型不匹配错误”。在我单击调试消息上的“结束”后,文件将按我所希望的那样关闭。我在未解决错误的行前面使用了“下一步恢复错误时的” 我正在打开的工作簿是活动工作簿,并且仍处于打开状态。我也尝试过使用工作簿(“”)。没有运气就关闭true 下面是我的代码: Option Explicit Option Private Mo

我在打开文件的主工作簿中运行代码;执行数据刷新;然后我想保存文件。在我尝试使用
ActiveWorkbook.Close True
保存工作簿之前,一切正常。我得到一个“运行时错误13类型不匹配错误”。在我单击调试消息上的“结束”后,文件将按我所希望的那样关闭。我在未解决错误的行前面使用了“下一步恢复错误时的

我正在打开的工作簿是活动工作簿,并且仍处于打开状态。我也尝试过使用
工作簿(“”)。没有运气就关闭true

下面是我的代码:

Option Explicit
Option Private Module


'CONSTANTS
Const sTEMP_PATH_NAME As Variant = "C:\Users\ta7464\Vivial\Budgets - Documents\2018 Forecast F8\AdminOnly\"
Const sTEMPLATE_WKBK As Variant = "BlankTemplate_V2.xlsm"



'VARIABLES


Public Sub DeployNewTemplates()

'1.  Turn off features
    'TurnOffFunctionality

'2.  Open the spreadsheet and activate Sheet2
    DeployTemplates

'3. Turn on features
    'TurnOnFunctionality

End Sub


Private Sub DeployTemplates()
    Dim sMainWkbk As Variant, sBlnkTemp As Variant, rDeptCds As Variant, DeptArr As Variant
    Dim i As Long, sDeptNo As String, sDeptNm As String, sWBName As Variant, sWBFullName As Variant
    Dim wbTemplate As Workbook, wbTarget As Workbook


    'Load array with list of valid department codes
    Set rDeptCds = shDepts.Range("A1").CurrentRegion
    DeptArr = rDeptCds.Value

    'Assign variable for the TemplateMgmtTool workbook
    sMainWkbk = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))
    sBlnkTemp = Left(sTEMPLATE_WKBK, (InStrRev(sTEMPLATE_WKBK, ".", -1, vbTextCompare) - 1))

    'Opens the blank template for population and saving

    Set wbTemplate = Workbooks.Open(Filename:=sTEMP_PATH_NAME & sTEMPLATE_WKBK)

    'Loop through all department numbers with actuals in the current year to customize and save a template for each
    For i = LBound(DeptArr) + 1 To UBound(DeptArr)
        sDeptNo = DeptArr(i, 1)
        sDeptNm = DeptArr(i, 2)
        sWBFullName = sDeptNo & "-" & sDeptNm & "_BT.xlsm"
        sWBName = sDeptNo & "-" & sDeptNm & "_BT"

        '1.  Save a copy of the blank template with proper naming convention
         Workbooks(sTEMPLATE_WKBK).Activate
         wbTemplate.SaveCopyAs PathName & sWBFullName
         Set wbTarget = Workbooks.Open(Filename:=PathName & sWBFullName)

        '2. Populate the shPL tab with the active department number

        Workbooks(sWBFullName).Worksheets("P&L").Range("C1").Value = DeptArr(i, 1)

        '3 Refresh the template connections and wait until complete before saving and closing
         wbTarget.Connections("Query - DeptNo").Refresh
         wbTarget.Connections("Query - CoRollUp").Refresh
         wbTarget.Connections("Query - Current Year Actuals").Refresh
         wbTarget.Connections("Query - DeptCode").Refresh
         wbTarget.Connections("Query - Oracle Headcount").Refresh
         wbTarget.Connections("Query - ButtsInSeats").Refresh


        '4 Save and close the newly created template
        On Error Resume Next
        ActiveWorkbook.Close True


    Next i

End Sub

活动工作簿可能不是您认为的工作簿。既然您有工作簿的对象(
wbTemplate
),为什么不使用它呢?运行时错误13在该代码行似乎很奇怪,但您已经有了对该工作簿的引用-是否
wbTarget.Close True
(或
wbTemplate
,不确定要关闭什么)会给您相同的错误?这确实有意义。我确信这就是答案。仍然会因相同的错误消息而崩溃。该工作簿的
ThisWorkbook
模块中是否有
工作簿\u BeforeClose
BeforeSave
事件处理程序?如果是这样,那可能是抛出错误的代码。哇,马修。这就是答案。出于好奇…为什么会引起问题?再次感谢你的帮助。