VB to determine if Worksheet exists

Rocky0201

Active Member
Joined
Aug 20, 2009
Messages
278
Hi,

I have two subs wigh are:

Sub 1 - cycling through a list in my Location Master sheet, I add a sheet with the sheet name defined in my Location Master sheet.

This works fine adding worksheets if the worksheet does not exist. The issue I have is determining if the worksheet already exisits. If no, add worksheet. If yes, update worksheet.

Sub 2 - cycling through my list in my Location Master sheet, I delete a worksheet with the sheet name defined in my Location Master sheet.

This works fine deleteing worksheets if the worksheet exisits. The issue I have is determining before the delete is the sheet actually exists.

I guess what I' asking is for a routine that I can call to set a switch if he Worksheet exists. I would pass this routine the same name I need to check.

This one routine would satisfy my need in both of the subs above.

Thanks.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try like this

Code:
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function

Sub test()
If Not WorksheetExists("Sheet1") Then
    MsgBox "Unable to proceed", vbExclamation, "Error"
    Exit Sub
Else
    Sheets("Sheet1").Delete
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,877
Members
452,949
Latest member
Dupuhini

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