Supprimer les lignes si différent de 2 motifs

Résolu/Fermé
PYGOS69 Messages postés 452 Date d'inscription jeudi 23 août 2012 Statut Membre Dernière intervention 10 octobre 2023 - 30 sept. 2021 à 17:31
PYGOS69 Messages postés 452 Date d'inscription jeudi 23 août 2012 Statut Membre Dernière intervention 10 octobre 2023 - 1 oct. 2021 à 09:13
Bonjour,

Je souhaite dans un fichier EXCEL, avec vba supprimer dans une colonne les motifs donc les lignes qui ne sont pas égal à :

"PE - TESTONS"
&
"autre - mot"

Dans la macro suivante, Si j'indique 1 motif cela fonctionne mais si je propose 2 motifs,
cela ne fonctionne pas...

Voici ce qui ne fonctionne pas (erreur de syntaxe?)

Sub Delssmotif1()
Application.ScreenUpdating = False

Dim i As Integer
Dim ssmotif As String
ssmotif = "PE - TESTONS";"autre - mot"
With ThisWorkbook.Sheets("donnees")
For i = .Range("X" & .Rows.Count).End(xlUp).Row To 2 Step -1
If .Range("X" & i).Value <> ssmotif Then
Rows(i).Delete
End If
Next i
End With
End Sub


Voici ce qui fonctionne :

Sub Delssmotif2()
Application.ScreenUpdating = False

Dim i As Integer
Dim ssmotif As String
ssmotif = "PE - TESTONS"
With ThisWorkbook.Sheets("donnees")
For i = .Range("X" & .Rows.Count).End(xlUp).Row To 2 Step -1
If .Range("X" & i).Value <> ssmotif Then
Rows(i).Delete
End If
Next i
End With
End Sub

Bien cordialement,


Configuration: Windows / Firefox 78.0
A voir également:

1 réponse

cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
30 sept. 2021 à 18:46
Bonjour,

comme ceci:

Sub Delssmotif2()
Application.ScreenUpdating = False
Dim i As Integer
With ThisWorkbook.Sheets("donnees")
For i = .Range("X" & .Rows.Count).End(xlUp).Row To 2 Step -1
If .Range("X" & i).Value <> "PE - TESTONS" And .Range("X" & i).Value <> "autre - mot" Then
Rows(i).Delete
End If
Next i
End With
End Sub


Voilà

1
PYGOS69 Messages postés 452 Date d'inscription jeudi 23 août 2012 Statut Membre Dernière intervention 10 octobre 2023 21
30 sept. 2021 à 20:34
Bonjour et merci cs_Le Pivert !

Cela fonctionne !

Si je devais remplacer "PE - TESTONS" par "PE"* pour dire tout ce qui commence par "PE". Auriez- vous la syntaxe svp ?
Merci encore pour votre aide !
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728 > PYGOS69 Messages postés 452 Date d'inscription jeudi 23 août 2012 Statut Membre Dernière intervention 10 octobre 2023
Modifié le 1 oct. 2021 à 08:28
voir ceci:

https://silkyroad.developpez.com/VBA/ManipulerChainesCaracteres/#LI-B

ce qui donne:

Option Explicit
Option Compare Text
'https://silkyroad.developpez.com/VBA/ManipulerChainesCaracteres/#LI-B
Sub Delssmotif2()
Application.ScreenUpdating = False
Dim i As Integer
With ThisWorkbook.Sheets("donnees")
For i = .Range("X" & .Rows.Count).End(xlUp).Row To 2 Step -1
If Left(.Range("X" & i).Value, 2) <> "PE" And .Range("X" & i).Value <> "autre - mot" Then
Rows(i).Delete
End If
Next i
End With
End Sub


Voilà

@+ Le Pivert
0
PYGOS69 Messages postés 452 Date d'inscription jeudi 23 août 2012 Statut Membre Dernière intervention 10 octobre 2023 21 > cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024
1 oct. 2021 à 09:13
Un grand Merci !

Bonne journée !
0