Delete Worksheet If It Exists

  • Does anyone have SIMPLE code for this that can be run in a normal Sub. My worksheet will always be named "Cleaned". I have seen some posts regarding Functions and other things that seem more complicated than necessary.
    Thanks.

    Ed
    [SIZE=6]*ficus*[/SIZE]
    Thanks to all for making this website work!

  • Re: Delete Worksheet If It Exists


    How about this?


  • Re: Delete Worksheet If It Exists


    Hmmm, I modified it a little bit, but it doesn't want to work.



    Code
    Dim Clnd As Worksheet
        For Each Clnd In ThisWorkbook.Worksheets
            If Clnd.Name = "Cleaned" Then
                Application.DisplayAlerts = False
                Clnd.Delete
                Application.DisplayAlerts = True
                Exit Sub
            End If
        Next Clnd

    Ed
    [SIZE=6]*ficus*[/SIZE]
    Thanks to all for making this website work!

  • Re: Delete Worksheet If It Exists


    Okay, well im not quite shore why you changed the notation to represent the sheet name because the code is looping through all the worksheets in the workbook not all all the worksheets in the workbook named cleaned. But anyway what ever suits you best the code is still correct and your the one using it.


    Try checking to make sure there is not a space after Cleaned on the worksheet tab. Or try creating a new sheet naming it test and deleting it with the code to see if that works.


    You could also try temprorarily removing the application.displayalerts part of the code to see what message you get.

  • Re: Delete Worksheet If It Exists


    Okay I downloaded your workbook and the code is working.


    But it may not be working as you need it to.


    Could you tell me what you are trying to do with the code.


    Your code creates a worksheet named "cleaned". At the moment you have the code set to delete the sheet cleaned before it is created. When do you want to delete the sheet?

  • Re: Delete Worksheet If It Exists


    try this:


    it will create your sheet run your code and then delete the sheet named cleaned at the end.


    Also you need to turn screenupdating back on at the end which I have done for you.


    And also, generally you dont need to select cells to work with them. You may want to look into that.


  • Re: Delete Worksheet If It Exists


    I can't have it delete the sheet at the end otherwise it defeats the purpose of the code that is running. "Cleaned" are the results that I need to look at.

    Ed
    [SIZE=6]*ficus*[/SIZE]
    Thanks to all for making this website work!

  • Re: Delete Worksheet If It Exists


    Try this:


  • Re: Delete Worksheet If It Exists


    Here is how I would do what you are trying to do:


  • Re: Delete Worksheet If It Exists


    Reafidy's solution looks very clean. I would only change this:


    Code
    For Each ws In ThisWorkbook.Worksheets 
            If ws.Name = "Cleaned" Then 
                Application.DisplayAlerts = False 
                ws.Delete 
                Application.DisplayAlerts = True 
            End If 
        Next ws


    to this:

    Code
    On error resume next
    ThisWorkbook.Worksheets("Cleaned").delete
    on error goto 0

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Delete Worksheet If It Exists


    Yes I agree handling the error if the sheet doesnt exsist is better that cycling through the sheets to see if it exsists even if there is only two sheets as you might want to add more.


    How ever you will still need to handle the alert so:


    Code
    On Error Resume Next 
    Application.DisplayAlerts = False 
    ThisWorkbook.Worksheets("Cleaned").delete 
    Application.DisplayAlerts = True
    On Error Goto 0
  • Re: Delete Worksheet If It Exists


    That looks like it just might work. I'll try tomorrow when I get in to work and let you know. Thanks a lot!

    Ed
    [SIZE=6]*ficus*[/SIZE]
    Thanks to all for making this website work!

  • Re: Delete Worksheet If It Exists


    Nope. No Luck. I thought it might help to make a seperate sub and run it from the other code but that didn't help either.

    Ed
    [SIZE=6]*ficus*[/SIZE]
    Thanks to all for making this website work!

  • Re: Delete Worksheet If It Exists


    It's not that the code bugs exactlly, it just doesn't delete the sheet. The code after that (which inserts a new sheet called "Cleaned") bugs.

    Ed
    [SIZE=6]*ficus*[/SIZE]
    Thanks to all for making this website work!

  • Re: Delete Worksheet If It Exists


    Why delete the sheet? Why not just clear or delete the contents if you're just going to re-add. Would save a step.


    ska

  • Re: Delete Worksheet If It Exists


    That's weird, I tested the attachment & I bugged on me.
    As far as clearing the sheet, I might have to do that, but at the same time, there should be a way to do this.

    Ed
    [SIZE=6]*ficus*[/SIZE]
    Thanks to all for making this website work!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!