|
-
Aug 31st, 2005, 10:05 AM
#1
Thread Starter
Member
[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.
-
Aug 31st, 2005, 10:43 AM
#2
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:
Dim i As Integer
DisplayAlerts = False
LoopReEnter:
For i = 1 to Sheets.Count
If Sheets(i).Name <> "MM" Or Sheets(i).Name <> "JE" Or Sheets(i).Name <> "JO" Then
Sheets(i).Delete
Goto LoopReEnter
End If
Next i
DisplayAlerts = True
Example 2 - Selecting then Deleting
VB Code:
Dim i As Integer
DisplayAlerts = False
For i = 1 to Sheets.Count
If Sheets(i).Name <> "MM" Or Sheets(i).Name <> "JE" Or Sheets(i).Name <> "JO" Then
Sheets(i).Select
End If
Next i
Selection.Delete
DisplayAlerts = True
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Aug 31st, 2005, 10:44 AM
#3
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:
Dim i As Integer
Application.DisplayAlerts = False
For i = 1 To Sheets.Count
If Sheets(i).Name = "MM" Or Sheets(i).Name = "JE" Or Sheets(i).Name = "JO" Then
'Skip
Else
Sheets(i).Delete
End If
Next
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 31st, 2005, 10:45 AM
#4
Re: Need to delete some worksheets and save others
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Aug 31st, 2005, 10:52 AM
#5
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.
-
Aug 31st, 2005, 10:58 AM
#6
-
Aug 31st, 2005, 11:08 AM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 31st, 2005, 11:19 AM
#8
Thread Starter
Member
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.
-
Aug 31st, 2005, 11:37 AM
#9
Re: Need to delete some worksheets and save others
Sorry second example should be..
VB Code:
Dim i As Integer
DisplayAlerts = False
For i = 1 to Sheets.Count
If Sheets(i).Name <> "MM" Or Sheets(i).Name <> "JE" Or Sheets(i).Name <> "JO" Then
Sheets(i).Select
End If
Next i
Selection.Delete
ActiveWindow.SelectedSheets.Delete
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
-
Aug 31st, 2005, 11:42 AM
#10
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:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 31st, 2005, 11:48 AM
#11
Thread Starter
Member
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.
-
Aug 31st, 2005, 12:22 PM
#12
Thread Starter
Member
Re: Need to delete some worksheets and save others
I reversed Rob Dog's loop and now it works like a charm. Thank much!
-
Aug 31st, 2005, 12:23 PM
#13
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 31st, 2005, 12:26 PM
#14
Thread Starter
Member
Re: Need to delete some worksheets and save others
VB Code:
Dim i As Integer
Application.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
'Skip
Else
Sheets(i).Delete
End If
Next
Application.DisplayAlerts = True
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|