Delete Worksheet If It Exists

  • Re: Delete Worksheet If It Exists


    First the button doesn't work, it says missing some attachment. Running it manually, the code that bugs is:

    Code
    Sheets.Add.Name = "Cleaned"

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

  • Re: Delete Worksheet If It Exists


    hmm... thats really weird.


    Did you just try the example I posted or did you copy and paste the code and button to your actual workbook?


    What excel version are you using?


    I just cannot understand why even the button didnt work it just has a link to the code....

  • Re: Delete Worksheet If It Exists


    I'm using 2000. Also, I just tried pasting it into my file, no luck.

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

  • Re: Delete Worksheet If It Exists


    Code
    Sheets.Add.Name = "Cleaned"


    I don't think that's valid syntax. How about instead

    Code
    Sheets.Add ' (Before:= or After:= can specify where)
    ActiveSheet.Name ="Cleaned"

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

  • Re: Delete Worksheet If It Exists


    No, that works the first time you do it. The reason that is bugging is because the next time you run the code, the sheet doesn't get deleted so it can't make a sheet called cleaned because it is already there.

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

  • Re: Delete Worksheet If It Exists


    Interesting; I don't see that syntax in the documentation, but it's neat that it works.


    Peculiar that you can't delete the sheet, though. Can you delete it from the Immediate window? From the user interface? Is the sheet or workbook protected?

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

  • Re: Delete Worksheet If It Exists


    There's no problem deleting it manually, no protection or anything. It just seems that code to delete it doesn't work, but I'm not sure why.

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

  • Re: Delete Worksheet If It Exists


    Hmmm. You put a breakpoint on the line of code that does the deletion, step to watch it execute the line, and the worksheet just stays there fat and happy?


    Did you say that you can do

    Code
    Worksheets("Cleaned").Delete

    successfully from the Immediate window?

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

  • Re: Delete Worksheet If It Exists


    I'm not sure I'm following what you're saying. Can you put it in terms a caveman can understand cause that's about my intelligence level.

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

  • Re: Delete Worksheet If It Exists


    Cavemen are now a protected species under the political correctness code.


    The Immediate window is where debug.print outputs appear. You can also type VBA code into the window and execute it. So type,

    Code
    Worksheets("Cleaned").Delete

    into it and see if it works.


    Do you know how to set a breakpoint and step though code?

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

  • Re: Delete Worksheet If It Exists


    no. I don't even know what you mean step code.

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

  • Re: Delete Worksheet If It Exists


    OK, open the VB editor, put the cursor on the line of code that's supposed to delete the worksheet, and press F9 to set a breakpoint. You'll see that the line highlights in some color according to your editor settings.


    Then do whatever you do to make the code run. Execution will halt before executing that line. Pressing F8 then executes code one line at a time. It's the process you use for stepping through code.


    So the question is, do you hit the breakpoint? And what happens when you execute the line?

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

  • Re: Delete Worksheet If It Exists


    Also note that putting an apostrophe in front of any lines of code disables that line from running until the apostrophe is removed.


    You might want to add like below when debugging:


    Code
    'On Error Resume Next 
    'Application.DisplayAlerts = False 
    ThisWorkbook.Worksheets("Cleaned").delete 
    'Application.DisplayAlerts = True 
    'On Error Goto 0


    This will not allow the code to "run over" any errors and also display alerts.

  • Re: Delete Worksheet If It Exists


    Hmmm, the code break didn't work unless I did it wrong, but I don't believe I did. Also, I just want to verify that the fact that the code is in my personal workbook wouldn't effect anything, right?


    Nevermind, got it (the break) to work. And I just realized that the fact that it's in the personal workbook is exactlly why it's not working. Sorry it took me so long to think about that. Guess I was dropped on my head as a child.

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

  • Re: Delete Worksheet If It Exists


    Actually, right now I'm trying to figure out how to make it look at the right workbook. If I can't figure it out I'll let you know. Thanks everyone.

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

  • Re: Delete Worksheet If It Exists


    Hmm. I have no idea how to trigger an event in Personal.xls from another workbook. You could call the routine in Personal from a change event in the workbook of interest, but then you might as well put the rest of the code in there -- plus no one else has your Personal.


    Let us know how you do.

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

Participate now!

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