VBA error select sheet

jakobt

Active Member
Joined
May 31, 2010
Messages
337
Have a code to select a sheet. If the sheet dont exist I want a message box telling the sheet dont exist. Otherwise want to select the sheet.

Sub Open_TB3()
Dim entity As String




Entity = InputBox("Select entity")

if error Sheets(entity).Select

then messagebox (The entity does not exist)

Else: Sheets(entity).Select


End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Maybe this?
Code:
Sub Open_TB3()
Dim entity As String
Dim ws As Worksheet


TryAgain:
    entity = InputBox("Select entity")
    
    On Error Resume Next
        Set ws = Worksheets(entity)
    On Error GoTo 0
    
    If ws Is Nothing Then
        If MsgBox("A sheet named: '" & entity & "' does not exist. Try again?", vbCritical + vbYesNo, "Error") = vbYes Then
            GoTo TryAgain
        End If
        Exit Sub
    End If
    
    'If we get here, sheet exists
    ws.Select
    
End Sub
 
Upvote 0
Try this.
Code:
Sub Open_TB3()
Dim entity As String

    entity = InputBox("Select entity")

    If IsError(Evaluate("ISREF('" & entity & "'!A1)")) Then
        MsgBox "The entity does not exist"
    Else
        Sheets(entity).Select
    End If

End Sub
 
Upvote 0
Thanks it doesnt work complete. It always some up with the statement the entity does not exists. Even though the entity variable is equal to one of the sheet names:
Can you explain what this line does: Specifically what does the ISREF mean?
If IsError(Evaluate("ISREF('" & entity & "'!A1")) Then
 
Upvote 0
Try this:
Code:
Sub Select_Sheet()
'Modified  10/10/2018  7:23:25 AM  EDT
Dim ans As String
On Error GoTo M
ans = InputBox("Enter The sheet name you want", , ActiveSheet.Name)
Sheets(ans).Select
Exit Sub
M:
MsgBox "The sheet name you entered was  " & ans & vbNewLine & "That sheet does not exist"
End Sub
 
Upvote 0
The code I posted will only work for sheets in the active workbook.

ISREF is an Excel worksheet function that returns TRUE if the argument passed to is is a valid range reference.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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