'Iterate to Find Listobject in a Workbook

I am trying to find a table ( ListObject ) from a excel workbook ( Lets say workbook 2 ) after opening the same through a VBA subroutine in another workbook ( Lets say workbook 1 ).

The code I tried is as follows ,

Sub B()
        Dim TBL_EMP As ListObject
        Dim strFile As Variant
        Dim WS_Count As Integer
        
        strFile = "File Path"
        Set WB_TRN = Workbooks.Open(strFile)
        
        WS_Count = WB_TRN.Worksheets.Count
        For n = 1 To WS_Count
                On Error GoTo Next_IT
                Set TBL_EMP = WB_TRN.Worksheets(n).ListObjects("EmployeeNameTbl")
                If Not TBL_EMP Is Nothing Then
                    MsgBox "Object Found"
                End If
    Next_IT:
        Next n
    End Sub

When I run the subroutine it iterate only through 2 sheets and gives error code 9 " ( Subscript Out of Range ) eventhough workbook 2 has 10 worksheets.

If I open the workbook 2 through file open dialogue box then the code works fine.

Please help me to solve this. Thank you in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source