Boucle + if then
j
Bonsoir à tous,
je demande votre aide pour un problème de macro. Elle ne marche pas. J'ai mis un commentaire pour indiquer à quel endroit elle se trouve et ce qu'elle doit faire.
Merci de votre aide.
A +
Bonjour
Utilise plutôt Find qu'une boucle, c'est plus rapide.
T
Bonjour,
En remplacement de la proc "CommandButton_Ajouter_Click" :
Private Sub CommandButton_Ajouter_Click()
Dim Plage As Range
Dim Cel As Range
Dim Lig As Long
Label_Civilite.ForeColor = RGB(0, 0, 0)
Label_Nom.ForeColor = RGB(0, 0, 0)
Label_Prenom.ForeColor = RGB(0, 0, 0)
Label_Adresse.ForeColor = RGB(0, 0, 0)
Label_Lieu.ForeColor = RGB(0, 0, 0)
Label_Pays.ForeColor = RGB(0, 0, 0)
If TextBox_Nom.Value = "" Then Label_Nom.ForeColor = RGB(255, 0, 0)
If TextBox_Prenom.Value = "" Then Label_Prenom.ForeColor = RGB(255, 0, 0)
If TextBox_Adresse.Value = "" Then Label_Adresse.ForeColor = RGB(255, 0, 0)
If TextBox_Lieu.Value = "" Then Label_Lieu.ForeColor = RGB(255, 0, 0)
If ComboBox_Pays.Value = "" Then Label_Pays.ForeColor = RGB(255, 0, 0)
With Worksheets("Annuaire")
Set Plage = .Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp))
Lig = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
Set Cel = Plage.Find(TextBox_Nom.Text, , xlValues, xlWhole)
If Not Cel Is Nothing Then
MsgBox "Erreur de saisie, nom déjà présent dans la base de données !"
TextBox_Nom.Text = ""
TextBox_Nom.SetFocus
Exit Sub
Else
.Cells(Lig, 1).Value = Switch(OptionButton1, "Mme", OptionButton2, "Mlle", OptionButton3, "M")
.Cells(Lig, 2).Value = TextBox_Nom.Text
.Cells(Lig, 3).Value = TextBox_Prenom.Text
.Cells(Lig, 4).Value = TextBox_Adresse.Text
.Cells(Lig, 5).Value = TextBox_Lieu.Text
.Cells(Lig, 6).Value = ComboBox_Pays.Text
OptionButton1 = True
TextBox_Nom.Text = ""
TextBox_Prenom.Text = ""
TextBox_Adresse.Text = ""
TextBox_Lieu.Text = ""
ComboBox_Pays.ListIndex = -1
End If
End With
End Sub