Consulting

Results 1 to 2 of 2

Thread: if sheets("sheet1") doesn't exist, then a message box will appear

  1. #1

    if sheets("sheet1") doesn't exist, then a message box will appear

    Hi,

    I want a macro will can perform the following function:

    if sheets("sheet1") doesn't exist, then a message box will appear.

    Could you please write me the macro?

    My macro doesn't work.....

    [VBA]
    If Sheets("sheet1") Is Nothing Then

    MsgBox prompt:="sheet1 doesn't exist"

    End If
    [/VBA]

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Something like (as sub or function):
    [VBA]
    Option Explicit

    Sub example()
    Dim wks As Worksheet
    Dim strMsg As String

    On Error Resume Next
    Set wks = ThisWorkbook.Worksheets("Sheet1")
    On Error GoTo 0

    If wks Is Nothing Then
    MsgBox """Sheet1"" does not exist", 0, vbNullString
    End If
    End Sub

    Sub example2()
    Const WORKSHEET_NAME As String = "Sheet1"
    If Not SheetExists(WORKSHEET_NAME) Then
    MsgBox """" & WORKSHEET_NAME & """ does not exist", 0, vbNullString
    End If
    End Sub

    Function SheetExists(ByVal ShName As String) As Boolean
    On Error Resume Next
    SheetExists = ThisWorkbook.Worksheets(ShName).Name = ShName
    End Function[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •