Checking If A Sheet Exists In An External Closed Workbook

前端 未结 3 1530
星月不相逢
星月不相逢 2021-01-14 20:11

I want to test whether certain sheets in the current workbook exist in another closed workbook and return a message saying which sheet/s are causing errors.

I prefe

相关标签:
3条回答
  • 2021-01-14 20:45

    Just an update for Tim's Function for error Handling:

    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
    
    0 讨论(0)
  • 2021-01-14 20:53

    Here's a slightly different approach:

    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
    
    0 讨论(0)
  • 2021-01-14 21:06

    Sub Tester()

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

    End Sub

    0 讨论(0)
提交回复
热议问题