XL 2019 Suppression ligne avec texte spécifique dans cellule

PETIT YANNICK

XLDnaute Occasionnel
Bonjour

Je souhaite supprimer les lignes contenant les textes SOLIDWORKS Drawing Document, STEP File, Foxit PhantomPDF PDF Document dans la colonne T

J'ai écrit adapté ce code mais il ne fonctionne pas et je n'arrive pas a trouver pourquoi.
Quelqu'un aurait une idée?


Sub suppression_ligne()



Dim nbligne As Long
nbligne = Range("B1").CurrentRegion.Rows.Count

For i = nbligne To 1 Step -1

If Cells(20, i).Value = "STEP File" Then
Selection.EntireRow.Delete

ElseIf Cells(20, i).Value = "SOLIDWORKS Drawing Document" Then
Selection.EntireRow.Delete

ElseIf Cells(20, i).Value = "PDF" Then
Selection.EntireRow.Delete

End If
Next i
End Sub
 

Pièces jointes

  • demande validation macro.xlsm
    22.4 KB · Affichages: 8

danielco

XLDnaute Accro
Bonjour,

Je vois deux erreurs :
- tu mets 20,I au lieu de i,20
- tu mets Selection alors que tu ne sélectionnes rien

VB:
Sub suppression_ligne()
Dim nbligne As Long
nbligne = Range("B1").CurrentRegion.Rows.Count
For i = nbligne To 1 Step -1
  If Cells(i, 20).Value = "STEP File" Then
    Cells(i, 20).EntireRow.Delete
  ElseIf Cells(20, i).Value = "SOLIDWORKS Drawing Document" Then
    Cells(i, 20).EntireRow.Delete
  ElseIf Cells(20, i).Value = "PDF" Then
    Cells(i, 20).EntireRow.Delete
  End If
Next i

Cordialement.

Daniel
 

James007

XLDnaute Barbatruc
Bonjour,

L'instruction Cells(i,j) a besoin de i en lignes et de j en colonnes ...

Tu peux tester la modification suivante :
VB:
Sub suppression_ligne()
 Dim nbligne As Long, i As Long
 nbligne = Range("B1").CurrentRegion.Rows.Count
    For i = nbligne To 2 Step -1
        If Cells(i, 20) = "STEP File" Or Cells(i, 20).Value = "SOLIDWORKS Drawing Document" _
            Or Cells(i, 20).Value = "PDF" Then
            Rows(i & ":" & i).EntireRow.Delete
        End If
    Next i
End Sub

En espèrant que cela t'aide
 

Hasco

XLDnaute Barbatruc
Repose en paix
Bonjour,

VB:
Sub suppression_ligne()
    Dim nbligne As Long
    Dim valeur As String
    nbligne = Range("B1").CurrentRegion.Rows.Count
    For i = nbligne To 1 Step -1
        valeur = Trim(Cells(i, 20).Value)
        If valeur = "STEP File" Or valeur = "SOLIDWORKS Drawing Document" _
           Or valeur Like "*PDF*" Then Cells(i, 20).EntireRow.Delete
    Next i
End Sub

Lorsqu'il y a problème, pensez à regarder l'aide excel. Par exemple sur 'Cells' qui vous aurez montré que c'est Cells(Ligne,Colonne) et non Cells(Colonne,Ligne)

A bientôt

[Edit] bonjour @James007, @danielco, @PETIT YANNICK n'aura que l'embarras du choix
 

Discussions similaires