PDA

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



uktous
06-21-2012, 05:05 AM
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.....


If Sheets("sheet1") Is Nothing Then

MsgBox prompt:="sheet1 doesn't exist"

End If

GTO
06-21-2012, 05:22 AM
Something like (as sub or function):

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