W
wachen
I use the following code to check if a worksheet exists. It works OK if the
worksheet does exist, However, if the worksheet does not exist, then
the "Set" statement gives a "Subscript out of range" error.
Can someone please help? Thanks.
Sub checkit ()
Dim NewTabName As String, WkSht As Worksheet
NewTabName = "1-23-04"
Set WkSht = Workbooks(CurBookName).Worksheets(NewTabName)
If Not WkSht Is Nothing Then
MsgBox "The worksheet exists"
Else
Workbooks(CurBookName).Activate
Workbooks(CurBookName).Sheets.Add.Name = NewTabName
End If
End Sub
worksheet does exist, However, if the worksheet does not exist, then
the "Set" statement gives a "Subscript out of range" error.
Can someone please help? Thanks.
Sub checkit ()
Dim NewTabName As String, WkSht As Worksheet
NewTabName = "1-23-04"
Set WkSht = Workbooks(CurBookName).Worksheets(NewTabName)
If Not WkSht Is Nothing Then
MsgBox "The worksheet exists"
Else
Workbooks(CurBookName).Activate
Workbooks(CurBookName).Sheets.Add.Name = NewTabName
End If
End Sub