Recherche et suppression ligne dans une feuil

kumpb Messages postés 78 Statut Membre -  
Bidouilleu_R Messages postés 1209 Statut Membre -
Bonjour,

Apres avoir mis dans une variable une chaîne de caractère ......

Dim Cible As String
Cible = "TextBoxSupNOM"

je voudrai rechercher dans une feuille excel la variable cible dans la colonne A et supprimer la ligne correspondante. Tout ceci en VBA.

voici ou j'en suis pour le moment :

Dim Cible As String 'définition d'une chaîne de texte
Cible = "TextBoxSupNOM"
Dim idxNom As Integer
Application.ScreenUpdating = False

Sheets("Personnel").Activate ' activation page de recherche
idxNom = 2 ' 1ère ligne de recherche

With Sheets("Personnel")
While .Range("A" & idxNom) <> cible 'test texte identique a chaîne recherchée
idxNom = idxNom + 1
Wend
MsgBox "trouvé"

Application.ScreenUpdating = True
End With

2tant novice en vba je compte sur vos conseils avisés

merci d'avance
A voir également:

1 réponse

Bidouilleu_R Messages postés 1209 Statut Membre 295
 
bonjour,

il y a un risque avec la boucle while..wend
si l'iformation recherchée n'existe pas on boucle jusqu'à 65536 puis erreur...
je te propose de regarder ce qui suit

Sub test()

Dim Cible As String 'définition d'une chaîne de texte
Cible = "TextBoxSupNOM"
Dim idxNom As Integer
Dim résultat As String
Dim DernièreLigne As Integer
Dim C As Variant


Application.ScreenUpdating = False

Sheets("Feuil1").Activate ' activation page de recherche
idxNom = 2 ' 1ère ligne de recherche

With Sheets("Feuil1")
DernièreLigne = [A65536].End(xlUp).Offset(1, 0).Row

For Each C In Range("A2:A" & DernièreLigne)
If C.Value <> Cible Then
résultat = " pas trouvé"

Else
résultat = "trouvé"
idxNom = C.Row ' le numero de ligne ou je trouve l'info ça peut servir.
Exit For
End If

Next

MsgBox résultat

Application.ScreenUpdating = True
End With

End Sub

A+
0