ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If Worksheet exists, delete it, then re-add it (https://www.excelbanter.com/excel-programming/438428-if-worksheet-exists-delete-then-re-add.html)

Cheryl

If Worksheet exists, delete it, then re-add it
 
Working in Excel 2007, I am writing a program to create a scoresheet to keep
track of scores for a game my family plays. Since a lot of the set-up is
based on the number of players, the first thing I want to do if check to see
if sheet called "Score" is there, left over from a previous game, and if so,
delete. The program will then create a new worksheet, "Score". I've seen a
lot of help on how to create a new worksheet if one doesn't already exist,
but not on how to delete an existing one, then add it again.

Can anyone help this newbie?

Thanks

Ryan H

If Worksheet exists, delete it, then re-add it
 
This should work for you. Hope this helps! If so, let me know, click "YES"
below.

Option Explicit

Sub DeleteSheet()

' delete sheet if it exists
On Error Resume Next
Sheets("Score").Delete
On Error GoTo 0

' add new sheet
Sheets.Add After:=Sheets(Sheets.Count)

' rename sheet
Sheets(Sheets.Count).Name = "Score"

End Sub
--
Cheers,
Ryan


"Cheryl" wrote:

Working in Excel 2007, I am writing a program to create a scoresheet to keep
track of scores for a game my family plays. Since a lot of the set-up is
based on the number of players, the first thing I want to do if check to see
if sheet called "Score" is there, left over from a previous game, and if so,
delete. The program will then create a new worksheet, "Score". I've seen a
lot of help on how to create a new worksheet if one doesn't already exist,
but not on how to delete an existing one, then add it again.

Can anyone help this newbie?

Thanks


Cheryl

If Worksheet exists, delete it, then re-add it
 
That worked perfectly!

Thank you.

"Ryan H" wrote:

This should work for you. Hope this helps! If so, let me know, click "YES"
below.

Option Explicit

Sub DeleteSheet()

' delete sheet if it exists
On Error Resume Next
Sheets("Score").Delete
On Error GoTo 0

' add new sheet
Sheets.Add After:=Sheets(Sheets.Count)

' rename sheet
Sheets(Sheets.Count).Name = "Score"

End Sub
--
Cheers,
Ryan


"Cheryl" wrote:

Working in Excel 2007, I am writing a program to create a scoresheet to keep
track of scores for a game my family plays. Since a lot of the set-up is
based on the number of players, the first thing I want to do if check to see
if sheet called "Score" is there, left over from a previous game, and if so,
delete. The program will then create a new worksheet, "Score". I've seen a
lot of help on how to create a new worksheet if one doesn't already exist,
but not on how to delete an existing one, then add it again.

Can anyone help this newbie?

Thanks



All times are GMT +1. The time now is 03:03 PM.

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