XL 2016 Suppression lignes si valeur cellule égale 0

KTM

XLDnaute Impliqué
Bonjour chers tous
Dans mon fichier joint ; je voudrais extraire et copier dans un nouveau classeur en supprimant les lignes dont le total est 0
j'ai élaboré une macro mais certains points ne fonctionnent pas bien
1- La suppression des lignes à total =0
2- La mise en page dans le nouveau classeur
Merci de bien vouloir corriger mon code ou me proposer mieux.
VB:
Sub COPIE()
Application.ScreenUpdating = False
Dim chemin As String
Dim fichier As String, f As Worksheet,cel as range,Ls as long
chemin = ThisWorkbook.Path & "\fact\"
If Dir(chemin, vbDirectory) = "" Then MkDir chemin
    Application.ScreenUpdating = False
    Set f = ActiveWorkbook.Worksheets("fiche")
    fichier = "Extrait"
    With f
    .UsedRange.Copy
    End With
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Workbooks.Add (xlWBATWorksheet)
    Application.EnableEvents = True
    With ActiveWorkbook
    Application.ScreenUpdating = False
        With .Worksheets(1).Cells(1)
            .PasteSpecial Paste:=xlPasteValues
            .PasteSpecial Paste:=xlPasteFormats
        End With
        Application.CutCopyMode = False
        ActiveWindow.DisplayGridlines = False
    Application.ScreenUpdating = False
    With .Sheets(1)
    Application.ScreenUpdating = False
    For Each cel In .Range("E5:E26")
    If cel.Value = 0 Then cel.Rows.Delete
    Next cel
    Ls = Range("A" & Rows.Count).End(xlUp).Row + 2
    .Columns("A:E").AutoFit
    .PageSetup.PrintArea = .Range("$A$1:$E$" & Ls).Address
    .PageSetup.Orientation = xlPortrait
    .PageSetup.FitToPagesTall = 1
    .PageSetup.FitToPagesWide = 1
    .PageSetup.RightFooter = "&P de &N"
    .PageSetup.LeftMargin = Application.InchesToPoints(0.118110236220472)
    .PageSetup.RightMargin = Application.InchesToPoints(0.118110236220472)
    End With

                     Application.ScreenUpdating = False
                     Application.DisplayAlerts = False
.SaveAs chemin & fichier, 51
.Close
End With
Set f = Nothing
End Sub
 

Pièces jointes

  • Classeur1.xlsm
    17.1 KB · Affichages: 18

mapomme

XLDnaute Barbatruc
Supporter XLD
Bonjour @KTM,

Premier point :
Pour des suppressions de lignes, on commence toujours par la dernière ligne pour remonter vers la première. On oublie donc la boucle for Each...Next qui parcourt le range de la première ligne à la dernière. On utilise plutôt une boucle For i = DernièreLigne to PremièreLigne ... Next i (avec Step -1) :
VB:
   For i = 26 To 5 Step -1
      If .Cells(i, "e") = 0 Then .Rows(i).Delete
   Next i


Deuxième point :
Autre erreur avec votre code initial :
monRange.Rows désigne l'ensemble des lignes du range monRange et en aucun cas les lignes entières sur l'ensemble des colonnes de la feuilles.

Dans votre cas, vous utilisez cel.rows. Comme cel est une cellule unique, l'ensemble des lignes de cel est la cellule cel elle-même. Quand vous exécutez cel.Rows.Delete, vous ne supprimez que la cellule cel et en aucun cas vous ne touchez à d'autre cellules de la ligne. Vous ne remontez donc que la colonne E. Vous pourriez utiliser cel.entirerow.delete pour supprimer une ligne entière.
 
Dernière édition:

fanfan38

XLDnaute Barbatruc
Bonjour
Ta macro corrigée
VB:
Sub Copie()
  Application.ScreenUpdating = False
  Dim FCopie As String, FColle As String, i As Long, dl As Long
  Dim chemin As String, fichier As String
  chemin = ThisWorkbook.Path & "\fact\"
  If Dir(chemin, vbDirectory) = "" Then MkDir chemin
  fichier = "Extrait"
  FCopie = ActiveWorkbook.Name
  Application.DisplayAlerts = False
  Application.EnableEvents = False
  Application.Workbooks.Add
  FColle = ActiveWorkbook.Name
  Workbooks(FCopie).Activate
  Sheets("fiche").Select
  Sheets("fiche").Copy After:=Workbooks(FColle).Sheets(1)
  Workbooks(FColle).Sheets(1).Delete
 With Workbooks(FColle).Sheets("fiche")
  dl = .Range("E" & Rows.Count).End(xlUp).Row
   For i = dl To 5 Step -1
     If .Range("E" & i).Value = 0 Then .Range("E" & i).EntireRow.Delete
   Next
End With
  ActiveWorkbook.SaveAs chemin & fichier
  Application.DisplayAlerts = True
  Application.EnableEvents = True
  Application.ScreenUpdating = True
End Sub
A+ François
 

job75

XLDnaute Barbatruc
Bonjour KTM, mapomme, fanfan38,

Comme ceci c'est plus propre :
VB:
Sub COPIE()
Dim chemin As String, fichier As String, i&
chemin = ThisWorkbook.Path & "\fact\"
If Dir(chemin, vbDirectory) = "" Then MkDir chemin
fichier = "Extrait"
Application.ScreenUpdating = False
Application.DisplayAlerts = False 'si le fichier existe déjà
ThisWorkbook.Sheets("fiche").UsedRange.Copy
With Workbooks.Add(xlWBATWorksheet)
    With .Sheets(1)
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
        With Intersect(.[E:E], .UsedRange.EntireRow)
            For i = .Count To 1 Step -1
                If .Cells(i) = 0 Then .Cells(i).EntireRow.Delete
            Next i
        End With
        .Columns.AutoFit
        ActiveWindow.DisplayGridlines = False
        .PageSetup.PrintArea = .UsedRange.Address
        .PageSetup.Orientation = xlPortrait
        .PageSetup.Zoom = False
        .PageSetup.FitToPagesTall = 1
        .PageSetup.FitToPagesWide = 1
        .PageSetup.RightFooter = "&P de &N"
        .PageSetup.LeftMargin = Application.InchesToPoints(0.118110236220472)
        .PageSetup.RightMargin = Application.InchesToPoints(0.118110236220472)
    End With
    .SaveAs chemin & fichier, 51
    .Close
End With
End Sub
A+
 

Pièces jointes

  • Classeur(1).xlsm
    20.8 KB · Affichages: 2

KTM

XLDnaute Impliqué
Bonjour @KTM,

Premier point :
Pour des suppressions de lignes, on commence toujours par la dernière ligne pour remonter vers la première. On oublie donc la boucle for Each...Next qui parcourt le range de la première ligne à la dernière. On utilise plutôt une boucle For i = 1 to ... Next i (avec Step -1) :
VB:
   For i = 26 To 5 Step -1
      If .Cells(i, "e") = 0 Then .Rows(i).Delete
   Next i


Deuxième point :
Autre erreur avec votre code initial :
monRange.Rows désigne l'ensemble des lignes du range monRange et en aucun cas les lignes entières sur l'ensemble des colonnes de la feuilles.

Dans votre cas, vous utilisez cel.rows. Comme cel est une cellule unique, l'ensemble des lignes de cel est la cellule cel elle-même. Quand vous exécutez cel.Rows.Delete, vous ne supprimez que la cellule cel et en aucun cas vous ne touchez à d'autre cellules de la ligne. Vous ne remontez donc que la colonne E. Vous pourriez utiliser cel.entirerow.delete pour supprimer une ligne entière.
Bien compris et Merci !!!
 

KTM

XLDnaute Impliqué
Bonjour KTM, mapomme, fanfan38,

Comme ceci c'est plus propre :
VB:
Sub COPIE()
Dim chemin As String, fichier As String, i&
chemin = ThisWorkbook.Path & "\fact\"
If Dir(chemin, vbDirectory) = "" Then MkDir chemin
fichier = "Extrait"
Application.ScreenUpdating = False
Application.DisplayAlerts = False 'si le fichier existe déjà
ThisWorkbook.Sheets("fiche").UsedRange.Copy
With Workbooks.Add(xlWBATWorksheet)
    With .Sheets(1)
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
        With Intersect(.[E:E], .UsedRange.EntireRow)
            For i = .Count To 1 Step -1
                If .Cells(i) = 0 Then .Cells(i).EntireRow.Delete
            Next i
        End With
        .Columns.AutoFit
        ActiveWindow.DisplayGridlines = False
        .PageSetup.PrintArea = .UsedRange.Address
        .PageSetup.Orientation = xlPortrait
        .PageSetup.Zoom = False
        .PageSetup.FitToPagesTall = 1
        .PageSetup.FitToPagesWide = 1
        .PageSetup.RightFooter = "&P de &N"
        .PageSetup.LeftMargin = Application.InchesToPoints(0.118110236220472)
        .PageSetup.RightMargin = Application.InchesToPoints(0.118110236220472)
    End With
    .SaveAs chemin & fichier, 51
    .Close
End With
End Sub
A+
Super Job75 !!
 

KTM

XLDnaute Impliqué
Bonjour
Ta macro corrigée
VB:
Sub Copie()
  Application.ScreenUpdating = False
  Dim FCopie As String, FColle As String, i As Long, dl As Long
  Dim chemin As String, fichier As String
  chemin = ThisWorkbook.Path & "\fact\"
  If Dir(chemin, vbDirectory) = "" Then MkDir chemin
  fichier = "Extrait"
  FCopie = ActiveWorkbook.Name
  Application.DisplayAlerts = False
  Application.EnableEvents = False
  Application.Workbooks.Add
  FColle = ActiveWorkbook.Name
  Workbooks(FCopie).Activate
  Sheets("fiche").Select
  Sheets("fiche").Copy After:=Workbooks(FColle).Sheets(1)
  Workbooks(FColle).Sheets(1).Delete
With Workbooks(FColle).Sheets("fiche")
  dl = .Range("E" & Rows.Count).End(xlUp).Row
   For i = dl To 5 Step -1
     If .Range("E" & i).Value = 0 Then .Range("E" & i).EntireRow.Delete
   Next
End With
  ActiveWorkbook.SaveAs chemin & fichier
  Application.DisplayAlerts = True
  Application.EnableEvents = True
  Application.ScreenUpdating = True
End Sub
A+ François
Merci beaucoup !!
 

job75

XLDnaute Barbatruc
La suppression des lignes peut prendre du temps, il vaut mieux filtrer le tableau source :
VB:
Sub COPIE()
Dim chemin As String, fichier As String
chemin = ThisWorkbook.Path & "\fact\"
If Dir(chemin, vbDirectory) = "" Then MkDir chemin
fichier = "Extrait"
Application.ScreenUpdating = False
Application.DisplayAlerts = False 'si le fichier existe déjà
With ThisWorkbook.Sheets("fiche").UsedRange
    .AutoFilter 5, "<>0" 'filtre automatique
    .Copy
    With Workbooks.Add(xlWBATWorksheet)
        With .Sheets(1)
            .Cells(1).PasteSpecial Paste:=xlPasteValues
            .Cells(1).PasteSpecial Paste:=xlPasteFormats
            .Cells(1).Select
            Application.CutCopyMode = False
            .Columns.AutoFit
            ActiveWindow.DisplayGridlines = False
            .PageSetup.PrintArea = .UsedRange.Address
            .PageSetup.Orientation = xlPortrait
            .PageSetup.Zoom = False
            .PageSetup.FitToPagesTall = 1
            .PageSetup.FitToPagesWide = 1
            .PageSetup.RightFooter = "&P de &N"
            .PageSetup.LeftMargin = Application.InchesToPoints(0.118110236220472)
            .PageSetup.RightMargin = Application.InchesToPoints(0.118110236220472)
        End With
        .SaveAs chemin & fichier, 51
        .Close
    End With
    .AutoFilter 'retire le filtrage
End With
End Sub
 

Pièces jointes

  • Classeur(2).xlsm
    20.5 KB · Affichages: 2
Dernière édition:

Discussions similaires

Réponses
4
Affichages
491

Statistiques des forums

Discussions
311 725
Messages
2 081 940
Membres
101 845
dernier inscrit
annesof