How do I use VBA to check it a sheet exists or has the correct name

hellfire45

Active Member
Joined
Jun 7, 2014
Messages
447
I need to check a workbook to make sure that certain worksheets exist and that they're named correctly. I'm been trying something like what is below but they aren't working. Any suggestions? Thank you again so much!

Code:
if Worksheets("Tasks") <> "" then
 MsgBox ("Tasks Sheet Either Missing or Named Incorrectly")
 Workbooks(docname4).Close False
Exit Sub

Also tried

Code:
If Not SheetExists("Tasks") Then 
MsgBox ("Tasks Sheet Either Missing or Named Incorrectly")
 Workbooks(docname4).Close False
Exit Sub
 

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

NeonRedSharpie

Well-known Member
Joined
Jul 14, 2014
Messages
1,678
Code:
    For x = 1 To ThisWorkbook.Sheets.Count
        If Sheets(x).Name = "Tasks" Then
            GoTo found
        End If
    Next x

MsgBox ("Tasks Sheet Either Missing or Named Incorrectly")
 Workbooks(docname4).Close False

found:

' code to continue
 

bbott

Well-known Member
Joined
Feb 5, 2010
Messages
2,350
There's probably a more elegant way, but this seems to work:

Code:
Sub TskSht()

    On Error GoTo 500
    Sheets("Tasks").Select
    Exit Sub
500
    MsgBox ("No such sheet")
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,109,033
Messages
5,526,368
Members
409,697
Latest member
christopherlewis1620

This Week's Hot Topics

Top