[VBA] Delete multiple Excel sheets

Opacho -  
headshoot444 Posted messages 1 Registration date   Status Member Last intervention   -
Hello,

As part of a project involving VBA, the user is required to create different sheets (for consultation purposes). Upon closing the workbook, I would like all the sheets created to be automatically deleted, but the problem is that I cannot know the names of these sheets and I am therefore stuck. I started a bit of code but quickly found myself at an impasse:

Sub Test()

Application.DisplayAlerts = False
nbfeuille = Worksheets.Count

For i = 4 To nbfeuille (I want to keep the first 4 sheets of the Excel workbook)
w = ActiveSheet.Name

Next i

End Sub

If you have any solutions to my problem, I would greatly appreciate it ^^

Thank you in advance.

Configuration: Windows Vista / Internet Explorer 7.0

2 answers

lermite222 Posted messages 9042 Status Contributor 1 199
 
Hello,
Only valid if the added sheets are done "After" (After)

Sub SuppFeuille() Dim i As Integer Application.DisplayAlerts = False For i = 5 To Sheets.Count Sheets(i).Delete Next i Application.DisplayAlerts = True End Sub

Otherwise, you need to test the names of the sheets you know.
See you later

Experience teaches more surely than advice. (André Gide)
If you bump into a pot and it sounds hollow, it's not necessarily the pot that is empty. ;-)(Confucius)
3
headshoot444 Posted messages 1 Registration date   Status Member Last intervention  
 
Sub Test()
Dim i As Integer
application.DisplayAlerts = False
For i = 5 To Sheets.Count
Sheets(5).Delete
Next i
End Sub

Replacing i with 5 works better.
0