determine if a worksheet exists

bobaol

Board Regular
Joined
Jun 3, 2002
Messages
188
Office Version
  1. 365
  2. 2003 or older
Platform
  1. Windows
Hello,

I want to find a way to determine if the worksheet "closed" currently exist in my workbook. If the worksheet does exist, then do nothing. If the worksheet does not exist, then insert a new worksheet and name it "closed"

I can insert now, but i don't know how to find if it currently exist.

Sub Macro1()
Sheets.Add
ActiveSheet.Select
ActiveSheet.Name = "closed"
End Sub

thanks in advance.

- bobaol
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
if the variable strWkshtName contains the name of the worksheet you are looking for:

<pre>
WSExist= False
Set objWorksheet = ActiveWorkbook.Sheets(strWkshtName)

If Err = 0 Then
WSExist= True
End If

</pre>
 
Upvote 0
Like this:

Code:
Dim WS As Worksheet
On Error Resume Next
Set WS = Worksheets("Closed")
If Err <> 0 Then
   Worksheets.Add.Name = "Closed"
End If
 
Upvote 0
If you don't mind a little bit of extra effort, create a reusable function which you can use in other projects. This function will determine if a certain worksheet exists in any open workbook. I edited your macro1 procedure to call the function properly.

Tom
<pre>

Sub Macro1()
Select Case shExists(ActiveWorkbook.Name, "closed")
Case 1: MsgBox "The workbook name given is invalid or has not yet been saved."
Case 2: Worksheets.Add.Name = "closed"
End Select
End Sub


Public Function shExists(wbName As String, shName As String) As Integer
'returns 0 if the sheet exists, 1 if the workbook is invalid,
'2 if the worksheet is invalid
Dim Test_Exist As Integer

On Error GoTo NoBook
Test_Exist = Workbooks(wbName).Windows.Count
On Error GoTo 0

On Error GoTo NoSheet
Test_Exist = Workbooks(wbName).Sheets(shName).Index

Exit Function
NoBook:
Err.Clear
shExists = 1
Exit Function

NoSheet:
Err.Clear
shExists = 2

End Function

</pre>
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

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