Excel VBA: Confirm Reference String

I have the following VBA code: ActiveSheet.Cells(2, 1) = "='C:\Test Folder\[Wkbk_Closed.xlsx]Sheet2'!$A$1"

If sheet2 does not exist in "Wkbk_Closed.xlsx" A "Select Sheet" dialog appears that asks me to select a sheet.

1) What VBA code do I use to disable the "Select Sheet" dialog.
2) What VBA code do I use to show a messagebox whenever the sheet can't be found (i.e., which initiates the "Select Sheet" dialog in part 1.)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


pls try
Sub TestIfSheetsExists()
    Set Cn = CreateObject("ADODB.Connection")
    Set Rs = CreateObject("ADODB.Recordset")
    strFile = "C:\Test Folder\[Wkbk_Closed.xlsx]"
    strFileCon = Replace(Replace(strFile, "[", ""), "]", "")
    strSheet = "Sheet2"

    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFileCon & _
            ";Extended Properties = 'Excel 12.0 Xml;HDR=YES';"
    Cn.Open strCon

    strSQL = "SELECT * FROM [" & strSheet & "$] ;"
    On Error Resume Next
    Rs.Open strSQL, Cn
    ErrNr = Err.Number
    On Error GoTo 0
    If ErrNr = 0 Then
        ActiveSheet.Cells(2, 1) = "='" & strFile & strSheet & " '!$A$1"
        MsgBox Chr(34) & strSheet & Chr(34) & "sheet not found"
    End If
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fabrice LambertFabrice LambertCommented:

avoid using objects such as ActiveWorkbook, ActiveSheet, ActiveCell, Selection ect ... as these objects are dependant on user actions and are by nature chaotic.
As a developper, you don't want to use chaotic objects.
Prefer referencing precisely the objects you intend to use.
Dim wb As Excel.Workbook
Set wb = ThisWorkbook.

Dim ws As Excel.Worksheet
Set ws = wb.Worksheets(1)
ws.Cells(2, 1) = "........."
Set ws = Nothing
Set wb = Nothing

Open in new window

And I do not recommend the On Error Resume Next statement either, as it hide potential errors and undefined behaviors.
ouestqueAuthor Commented:
Thank You!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.