检查外部closures的工作簿中是否存在工作表

我想testing当前工作簿中的某些工作表是否存在于另一个已closures的工作簿中,并返回一条消息说明哪些工作表正在导致错误。

我不想打开/closures工作簿,所以我试图改变随机单元格中的公式以链接到文件path(fp)的工作簿来testing工作表是否存在。

我已经testing了这个虚拟工作表,我知道不存在于其他工作簿,它的工作原理,但是当我有多个表导致错误,我得到一个“应用程序定义或对象定义的错误”。 在第二次迭代中,我相信error handling的写法会导致崩溃,但我不完全明白这是如何工作的。

我得到的代码是:

Sub SheetTest(ByVal fp As String) Dim i, errcount As Integer Dim errshts As String For i = 2 To Sheets.Count On Error GoTo NoSheet Sheets(1).Range("A50").Formula = "='" & fp & Sheets(i).Name & "'!A1" GoTo NoError NoSheet: errshts = errshts & "'" & Sheets(i).Name & "', " errcount = errcount + 1 NoError: Next i Sheets(1).Range("A50").ClearContents If Not errshts = "" Then If errcount = 1 Then MsgBox "Sheet " & Left(errshts, Len(errshts) - 2) & " does not exist in the Output file. Please check the sheet name or select another Output file." Else MsgBox "Sheets " & Left(errshts, Len(errshts) - 2) & " do not exist in the Output file. Please check each sheet's name or select another Output file." End If End End If End Sub 

希望你们能帮助我,谢谢!

这是一个稍微不同的方法:

 Sub Tester() Dim s As Worksheet For Each s In ThisWorkbook.Worksheets Debug.Print s.Name, HasSheet("C:\Users\blah\Desktop\", "temp.xlsm", s.Name) Next s End Sub Function HasSheet(fPath As String, fName As String, sheetName As String) Dim f As String f = "'" & fPath & "[" & fName & "]" & sheetName & "'!R1C1" HasSheet = Not IsError(Application.ExecuteExcel4Macro(f)) End Function 

只是Tim的error handlingfunction的更新:

VBA:

 Function HasSheet(fPath As String, fName As String, sheetName As String) On Error Resume Next Dim f As String f = "'" & fPath & "[" & fName & "]" & sheetName & "'!R1C1" HasSheet = Not IsError(Application.ExecuteExcel4Macro(f)) If Err.Number <> 0 Then HasSheet = False End If On Error GoTo 0 End Function 

分testing仪()

MsgBox(Not IsError(Application.ExecuteExcel4Macro(“'C:\ temp [temp.xlsm] Sheetxyz'!R1C1”)))

结束小组