将工作簿对象传递给另一个函数时出错(运行时“自动化错误”)

时间:2016-02-25 15:58:24

标签: excel vba excel-vba

我有一组函数用于从单独的工作簿中的各种工作表中读取数据。主例程使用一个循环来遍历各种工作表名称,并在函数内两个级别下来,它查看工作簿以获取数据。我在这个函数的不同点得到runtime error '-2147023174' (800706ba) Automation error,我想知道错误在哪里。

Sub mainRoutine()
    'opens workbook and runs makeFile function on a loop
    Dim wbOBJ As Workbook, oFile As integer
    Set wbOBJ = Workbooks.Open(filePath, ReadOnly:=True)
    For k = 1 To 20                             
        makeFile myBox.List(k), wbOBJ
    Next k  
    wbOBJ.Saved = True
    wbOBJ.Close
End Sub

Function makeFile(ShtName As String, ByRef wbOBJ as Workbook)
    'calls various procedures from which to collect data
    'and passes workbook object along
    debug.print wbOBJ.Sheets(1).Cells(1,1).Value
    printInfoA(ShtName)
    printInfoB(ShtName, wbOBJ)
End Function

Function printInfoB(ShtName as String, ByRef wbOBJ as Workbook)
    'assigns object to a sheet inside the other workbook, collects data
    Dim wsOBJ As Worksheet
    Set wsOBJ = wbOBJ.Sheets(ShtName)
    For j = 1 to 10
        Thisworkbook.Sheets(1).Cells(j,1) = wsObj.Cells(j, 1)
    Next j
End Function

即使在makeFile函数中,即使debug.print有效,debug.print行上也会弹出错误,并生成正确的值。 如果我取消该行,则会在mainRoutine行的wbOBJ.Saved = True上发生相同的错误。造成这些错误的原因是什么?

(请注意,我最初尝试以这种方式组织它,而不是每次运行printInfoB函数时都打开和关闭工作簿,因为我正在尝试提高速度。)

0 个答案:

没有答案