How do I check if a sheetname exists in a file?

G

Guest

Hello experts, I am a real novice so be patient please. I want to create a
script that allows me to check whether a particular worksheet exists in a
workbook. At the moment my script assumes 'sheet 1' exists:

[Sheets("Sheet 1").Delete]

However, if the sheet has been manually deleted by the user my macro crashes.
 
N

NickHK

Here's one way:
Dim WS As Worksheet
On Error resume Next
Set WS=Thisworkbook.Sheets("Sheet 1")
'You may want an Application.displayalerts=false/true
If Not WS is Nothing then WS.delete
On error goto 0

NickHK

NezRhodes said:
Hello experts, I am a real novice so be patient please. I want to create a
script that allows me to check whether a particular worksheet exists in a
workbook. At the moment my script assumes 'sheet 1' exists:

[Sheets("Sheet 1").Delete]

However, if the sheet has been manually deleted by the user my macro
crashes.
 
G

Guest

Thanks Nick, it is perfect & much appreciated

NickHK said:
Here's one way:
Dim WS As Worksheet
On Error resume Next
Set WS=Thisworkbook.Sheets("Sheet 1")
'You may want an Application.displayalerts=false/true
If Not WS is Nothing then WS.delete
On error goto 0

NickHK

NezRhodes said:
Hello experts, I am a real novice so be patient please. I want to create a
script that allows me to check whether a particular worksheet exists in a
workbook. At the moment my script assumes 'sheet 1' exists:

[Sheets("Sheet 1").Delete]

However, if the sheet has been manually deleted by the user my macro
crashes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top