How to use VBA to check if sheet within an external file exists - Using Excel 2010

cwunderlich

Board Regular
Joined
Sep 24, 2010
Messages
101
I am creating VBA code. I need to check to see if a certain sheet exists within an external file. I would like to do this without opening up the external file.

I am currently using this code:

Code:
Private Function CheckForSheet(ByVal wbPath As String, wbName As String, wsName As String) As Variant
    Dim arg As String
    CheckForSheet = ""
    If Right(wbPath, 1) <> "\" Then wbPath = wbPath & "\"
    If Dir(wbPath & "\" & wbName) = "" Then Exit Function
    arg = "'" & wbPath & "[" & wbName & "]" & _
        wsName & "'!" & Range("A1").Address(True, True, xlR1C1)
    On Error Resume Next
    CheckForSheet = ExecuteExcel4Macro(arg)
End Function

However, this does not seem to always work. I am testing this on a workbook which I know has the the existing sheet name, but the ExecuteExcel4Macro(arg) is not returning anything.

Any ideas?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
It looks like it *might* be related to the scenario when the external workbook only has one sheet in it??? I am not sure though..
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,393
Members
449,446
Latest member
CodeCybear

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top