Delete a worksheet and recreate with the same sheet name

Adrian Gornall picture Adrian Gornall · Feb 7, 2014 · Viewed 11.3k times · Source

I'm currently trying to delete a worksheet and auto create a new worksheet with the same name.

I'm using the below code. However, when i run the code the windows pop up appears asking me to confirm deletion, i want to prevent this and just delete and replace with a new sheet. I want to avoid Send-keys for this.

ThisWorkbook.Sheets("Process Map").delete
ThisWorkbook.Sheets.Add.Name = "Process Map"

Answer

AER picture AER · Feb 7, 2014

Try setting the DisplayAlerts to False (then back to true if that is what you want as the default setting.

Application.DisplayAlerts = False
On Error Resume Next 
ThisWorkBook.Sheets("Process Map").Delete 
On Error Goto 0 
Application.DisplayAlerts = True
ThisWorkbook.Sheets.Add.Name = "Process Map" 

Or without error handling:

Application.DisplayAlerts = False
ThisWorkBook.Sheets("Process Map").Delete 
Application.DisplayAlerts = True
ThisWorkbook.Sheets.Add.Name = "Process Map"