ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How do I check if a sheetname exists in a file? (https://www.excelbanter.com/excel-programming/371080-how-do-i-check-if-sheetname-exists-file.html)

NezRhodes

How do I check if a sheetname exists in a file?
 
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.

NickHK

How do I check if a sheetname exists in a file?
 
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" wrote in message
...
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.



NezRhodes

How do I check if a sheetname exists in a file?
 
Thanks Nick, it is perfect & much appreciated

"NickHK" wrote:

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" wrote in message
...
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.





All times are GMT +1. The time now is 08:53 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com