Worksheet existence


Posted by Fred Schmid on June 14, 2001 8:34 AM

How do you check for the existence of a worksheet in a workbook in a macro? I want to delete a certain worksheet if it exists. If I try to delete the worksheet and it doesn't exist I get a 'Subscript out of range' error. Also, similiar to this how do I check for an open workbook. If I try to copy something to a workbook that is not open I get an error. If I open the workbook and it is already open I get an error.
Thanks,

Posted by AB on June 14, 2001 11:08 AM

Application.DisplayAlerts = False
For Each sh In ActiveWorkbook.Sheets
If sh.Name = "Sheet1" Then sh.Delete
Next sh


Regards,
AaronThe Excel Logic Page



Posted by Just a thought on June 14, 2001 12:41 PM

Don't know if this will help, But I came across a Function about a month ago & started using it. It can be found @ the following site - (Look for "Check for Existence of a sheet before Creating" -about a quarter down).
http://www.geocities.com/davemcritchie/excel/sheets.htm

Don't know which is faster Function or For each sht. But I kinda of like the Function. Once it's created, you can incorporate it into any other macro.

Just a thought.