XL 2016 Erreur VBA Suppresion des onglets

sive91

XLDnaute Junior
Bonjour,

je n'arrive pas a faire fonctionner mon code, j'ai une erreur
La methode Delete de la Classe Worksheet à echouée
l'erreur s'affiche sur Sheets(i).Delete

Voici le code

Private Sub CommandButton4_Click()
Dim s As Worksheet, t As String
Dim i As Long, K As Long
K = Sheets.Count

For i = K To 1 Step -1
t = Sheets(i).CodeName
If t = "Feuil1" Or t = "Feuil2" Then
Application.DisplayAlerts = False
Application.DisplayAlerts = True
Else
Application.DisplayAlerts = False
Sheets(i).Delete
Application.DisplayAlerts = True
End If
Next i
End Sub

Merci
 

sive91

XLDnaute Junior
Parfait cela fonctionne, merci a tous

Private Sub CommandButton4_Click()
Dim Sh As Worksheet
Application.DisplayAlerts = False
For Each Sh In ThisWorkbook.Worksheets
Sh.Visible = xlSheetVisible
If Sh.CodeName <> "Feuil1" And Sh.CodeName <> "Feuil2" And Sh.CodeName <> "Feuil3" Then Sh.Delete

Next
For Each Sh In ThisWorkbook.Worksheets
If Sh.CodeName = "Feuil3" Then Sh.Visible = xlSheetVeryHidden
Next

Application.DisplayAlerts = True
End Sub
 

Jacky67

XLDnaute Barbatruc
RE...
Je ferais plutôt comme ceci
Retifié, je n'ai pas vu que"feuil3" était concernée
Code:
Private Sub CommandButton4_Click()
Dim Sh As Worksheet
With Application: .DisplayAlerts = False: .ScreenUpdating = False: End With
For Each Sh In ThisWorkbook.Worksheets
With Sh
If .CodeName <> "Feuil1" And .CodeName <> "Feuil2" And .CodeName <> "Feuil3" Then
.Visible = xlSheetVisible
.Delete
End If
End With
Next
With Application: .DisplayAlerts = True: .ScreenUpdating = True: End With
End Sub
Ainsi la propriété "Visible "de "Feuil1" , feuil2" et feuil3 n'est pas affectée.
Quant à
*****/
For Each Sh In ThisWorkbook.Worksheets
If Sh.CodeName = "Feuil3" Then Sh.Visible = xlSheetVeryHidden
Next
*****/
Ce code devient inutile
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 165
Messages
2 085 882
Membres
103 011
dernier inscrit
rine