Results 1 to 14 of 14

Thread: [RESOLVED] Need to delete some worksheets and save others

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Resolved [RESOLVED] Need to delete some worksheets and save others

    I am new to VB, but fortunately you guys/gals have been extremely helpful.

    I am working on a macro for excel. When played in a workbook I want the macro to ignore three worksheets that I am saving “MM”, “JE”, and “JO” but delete the rest. The worksheets that need to be deleted vary in amount and name.

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Need to delete some worksheets and save others

    If the worksheets that you are wishing to save never change then this can be achieved by looping around the sheets collection and deleting sheets that do not match criteria.

    Two ways..

    Example 1 - Deleting as you go

    VB Code:
    1. Dim i As Integer
    2.   DisplayAlerts = False
    3. LoopReEnter:
    4.   For i = 1 to Sheets.Count
    5.     If Sheets(i).Name <> "MM" Or Sheets(i).Name <> "JE" Or Sheets(i).Name <> "JO" Then
    6.       Sheets(i).Delete
    7.       Goto LoopReEnter
    8.     End If
    9.   Next i
    10.   DisplayAlerts = True

    Example 2 - Selecting then Deleting

    VB Code:
    1. Dim i As Integer
    2.   DisplayAlerts = False
    3.   For i = 1 to Sheets.Count
    4.     If Sheets(i).Name <> "MM" Or Sheets(i).Name <> "JE" Or Sheets(i).Name <> "JO" Then
    5.       Sheets(i).Select
    6.     End If
    7.   Next i
    8.   Selection.Delete
    9.   DisplayAlerts = True
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need to delete some worksheets and save others

    In your macro code do a test for the sheet name and if its one of the three then skip deleting.
    VB Code:
    1. Dim i As Integer
    2. Application.DisplayAlerts = False
    3. For i = 1 To Sheets.Count
    4.     If Sheets(i).Name = "MM" Or Sheets(i).Name = "JE" Or Sheets(i).Name = "JO" Then
    5.         'Skip
    6.     Else
    7.         Sheets(i).Delete
    8.     End If
    9. Next
    10. Application.DisplayAlerts = True
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Need to delete some worksheets and save others

    Me Win.. with more..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Need to delete some worksheets and save others

    Its easier to iterate backwards through your sheets this way you won't get into trouble due to reindexing sheets in a sheet deletion. ( the sheet after the sheet you delete will get the index of the deleted sheet)
    I hope it makes sence to you

    Code:
    Dim i As Integer
    DisplayAlerts = False
    
      For i = Sheets.Count to 1 step -1
        If Sheets(i).Name <> "MM" Or Sheets(i).Name <> "JE" Or Sheets(i).Name <> "JO" Then
          Sheets(i).Delete
              End If
      Next i
    DisplayAlerts = True
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  6. #6
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Need to delete some worksheets and save others

    Anymore for anymore??
    BTW Dnereb.. you following me?? and I also covered that off in my first example.. notice the goto statement
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need to delete some worksheets and save others

    Yes, I forgot the reverse looping. Oh well. Easy enough to take care of though.

    You can "win" Danny although it not a competition. Its just helping out members with their issues.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Need to delete some worksheets and save others

    It could be a problem on my end but,

    Danny, I played with both of your sets of code, the first deletes all of the worksheets, the second deletes a cell if a cell is selected then moves to the end worksheet and stops.

    RobDog, your code does save and delete the correct worksheets, but goes into debug mode depending on the position of the saved and deleted worksheets. Also, what code did you have in mind for skipping?

    Dnereb like Danny your code deletes all of the worksheets.

  9. #9
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Need to delete some worksheets and save others

    Sorry second example should be..

    VB Code:
    1. Dim i As Integer
    2.   DisplayAlerts = False
    3.   For i = 1 to Sheets.Count
    4.     If Sheets(i).Name <> "MM" Or Sheets(i).Name <> "JE" Or Sheets(i).Name <> "JO" Then
    5.       Sheets(i).Select
    6.     End If
    7.   Next i
    8.   Selection.Delete
    9.   ActiveWindow.SelectedSheets.Delete
    10.   DisplayAlerts = True

    My First example should of worked fine as it is just the reverse of RD's, it should only delete the sheet if the name does not match..

    RD

    I know just kidding..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need to delete some worksheets and save others

    Yes, as I mentioned in my last post. You need to reverse the loop by...
    VB Code:
    1. For i = Sheets.Count To 1 Step -1
    Ps, Danny, I know you are but others may not.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Need to delete some worksheets and save others

    Thanks for the quick reply, but this time it deleted the end sheet which happened to be "JE" then stopped. Perhaps it is something on my end; but I have tripled checked the names, and the sheets to be deleted are just inserted sheets that I am using to hammer on.

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Need to delete some worksheets and save others

    I reversed Rob Dog's loop and now it works like a charm. Thank much!

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need to delete some worksheets and save others

    Post the code your using so we are all on the same page.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    61

    Re: Need to delete some worksheets and save others

    VB Code:
    1. Dim i As Integer
    2. Application.DisplayAlerts = False
    3. For i = Sheets.Count To 1 Step -1
    4.     If Sheets(i).Name = "MM" Or Sheets(i).Name = "JE" Or Sheets(i).Name = "JO" Then
    5.         'Skip
    6.     Else
    7.         Sheets(i).Delete
    8.     End If
    9. Next
    10. Application.DisplayAlerts = True
    11.      
    12.     Set ws = Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width