Public Reponse As Variant
' code avec plage variable mais supprime sans validation
Sub supprimer_enregistrements_quand_pays_sans_confirmation()
Dim DLigP As Long, LigP As Long, ShtP As Worksheet
Dim DLigB As Long, LigB As Long
Dim pays_à_supprimer As String
Set ShtP = Sheets("Paramètres")
DLigP = ShtP.Range("B" & Rows.Count).End(xlUp).Row
' Pour chaque pays à supprimer
For LigP = 3 To DLigP
pays_à_supprimer = ShtP.Range("B" & LigP)
' Avec la feuille Base
With Sheets("Base")
.Activate
' Memoriser la dernière ligne
DLigB = .Range("B" & Rows.Count).End(xlUp).Row
' Pour chaque ligne en partant de la fin
For LigB = DLigB To 2 Step -1
' SI le pays correspond, supprimer la ligne
If .Range("B" & LigB) = pays_à_supprimer Then Rows(LigB).Delete
Next LigB
End With
Next LigP
End Sub
' code avec plage variable mais demande confirmation avant suppression
Sub supprimer_enregistrements_quand_pays_avec_confirmation_préalable()
Dim DLigP As Long, LigP As Long, ShtP As Worksheet
Dim DLigB As Long, LigB As Long
Dim pays_à_supprimer As String
Set ShtP = Sheets("Paramètres")
DLigP = ShtP.Range("B" & Rows.Count).End(xlUp).Row
' Pour chaque pays à supprimer
For LigP = 3 To DLigP
pays_à_supprimer = ShtP.Range("B" & LigP)
' suit la question mais ne fonctionne pas !
Reponse = MsgBox("voulez-vous vraiment supprimer " & pays_à_supprimer & " ?", vbOKCancel, "Question")
If Reponse = vbOK Then
With Sheets("Base")
.Activate
' Memoriser la dernière ligne
DLigB = .Range("B" & Rows.Count).End(xlUp).Row
For LigB = DLigB To 2 Step -1
' SI le pays correspond, supprimer la ligne
If .Range("B" & LigB) = pays_à_supprimer Then Rows(LigB).Delete
Next LigB
End With
End If
Next LigP
End Sub
' code sans plage variable mais supprime sans demander confirmation
' je préfère bien sûr le code avec zone variable mais c'est pour plus de clarté
' que je fais figurer cette version ici (supprimer_enregistrements_quand_pays ... a ma préférence)
Sub supprimer_quand_critère_sans_confirmation()
Dim i As Long
Dim pays_à_supprimer As Range
Dim Cellule As Range
Set pays_à_supprimer = Sheets("Paramètres").Range("B3:B12")
For Each Cellule In pays_à_supprimer
With Sheets("Base")
For i = .UsedRange.Rows.Count To 2 Step -1
If .Cells(i, 2) = Cellule.Value Then Rows(i).Delete
Next i
End With
Next
End Sub
' code sans plage variable mais demande confirmation avant suppression
' je préfère bien sûr le code avec zone variable mais c'est pour plus de clarté
' que je fais figurer cette version ici (supprimer_enregistrements_quand_pays ... a ma préférence)
Sub supprimer_quand_critère_avec_confirmation_préalable()
Dim i As Long
Dim pays_à_supprimer As Range
Dim Cellule As Range
Set pays_à_supprimer = Sheets("Paramètres").Range("B3:B12")
For Each Cellule In pays_à_supprimer
Reponse = MsgBox("voulez-vous vraiment supprimer " & pays_à_supprimer & " ?", vbOKCancel, "Question")
If Reponse = vbOK Then
With Sheets("Base")
For i = .UsedRange.Rows.Count To 2 Step -1
If .Cells(i, 2) = Cellule.Value Then Rows(i).Delete
Next i
End With
End If
Next
End Sub