Adding a Sheet if It's Not There to Delete

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Code:
For Each sht In ActiveWorkbook.Worksheets
    If sht.Visible And (sht.Name = "NonFormat") Then
        sht.Delete
    End If
Next sht
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "NonFormat"

Hello All,
The following will delete the NonFormat sheet, and then add a new sheet...whichis what I want. What if there is no NonFormat sheet to delete, it will fault on me.
How do I get excel to automatically add the NonFormat sheet, if one does not exist to delete?
Thanks for the help
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Last edited:
Upvote 0
You asked this:
How do I get excel to automatically add the NonFormat sheet, if one does not exist to delete?
You can use the code contained it that links that shows you how to check if a sheet exists or not.
So you can use an IF statement to see if a sheet by that names exists, and if not, you can add the sheet.
 
Upvote 0
Code:
Dim wSht As Integer
wSht = Application.Sheets.Count
worksheetexists = False
    For x = 1 To wSht
    If Worksheets(x).Name = "NonFormat" Then
        worksheetexists = True
    Exit For
    End If

    Next x
    If worksheetexists = False Then
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "NonFormat"
'    Exit Sub
    End If

I'm using the code above, which is working, however, I'm getting a variable not defined in "worksheetexists = False". How do I define this variable?
Thanks
Also, the code above does work without Option Explicit.
Thanks for the help
 
Last edited:
Upvote 0
How about
Code:
Sub AddSheet()
   If Not Evaluate("isref(NonFormat!A1)") Then Sheets.Add(, Sheets(Sheets.count)).Name = "NonFormat"
End Sub
 
Upvote 0
I'm using the code above, which is working, however, I'm getting a variable not defined in "worksheetexists = False". How do I define this variable?
Thanks
Also, the code above does work without Option Explicit.
With "Option Explicit" turned on (which is recommended), you are required to declare variable before you use them (with a "Dim" statement).
You already did that with your wSht variable. You need to do the same with the worksheetexists variable, like this:
Code:
Dim worksheetexists as Boolean
 
Last edited:
Upvote 0
Here is what I use to see if a sheet exist:
Code:
Sub Does_Sheet_Exist()
'Modified  8/30/2018  9:14:28 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim x As Long
x = 0
    For i = 1 To Sheets.Count
        If Sheets(i).Name = "Me" Then x = 1
    Next
If x = 0 Then Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Me"
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Code:
Dim worksheetexists as Boolean

That's what I was looking for
Thanks to all
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top