View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Charlotte E[_2_] Charlotte E[_2_] is offline
external usenet poster
 
Posts: 38
Default If named sheet exists then delete it


Instead of all these functions, loops, and other 'advanced' solutions you
often see for solving this problem, I would take advantage of the fact, that
Excel always activate and jump to any newly added sheet, and then check if
that sheetname is 'Test'.

The macro would look something like this:
__________________________________________________ __________
On Error Resume Next ' If sheet exsists
Application.DisplayAlerts = False ' If sheet must be deleted

Sheets.Add.Name = "Test"
If ActiveSheet.Name < "Test" Then ActiveSheet.Delete
Sheets("Test").Select
__________________________________________________ __________

Only 3 quick lines, but it gets the job done :-)

CE


"Pete_UK" wrote in message
...
I am creating a new sheet in a macro and then renaming it to Test with
the following lines:

Sheets.Add
ActiveSheet.Name = "Test"

However, there might already be a sheet called Test in the workbook
from an earlier (failed) run of the macro. How can I detect if that
sheet exists and delete it if it does exist before the above lines of
code?

Many thanks,

Pete