Private Sub CommandButton_Ajouter_Click()
'Coloration des Labels en noir
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)
'Contrôles de contenu
If TextBox_Nom.Value = "" Then 'SI pas de "nom" ...
Label_Nom.ForeColor = RGB(255, 0, 0) 'Label "nom" en rouge
ElseIf TextBox_Prenom.Value = "" Then
Label_Prenom.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Adresse.Value = "" Then
Label_Adresse.ForeColor = RGB(255, 0, 0)
ElseIf TextBox_Lieu.Value = "" Then
Label_Lieu.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_Pays.Value = "" Then
Label_Pays.ForeColor = RGB(255, 0, 0)
Else
'Si le formulaire est complet, on insère les valeurs sur la feuille
Dim no_ligne As Integer, civilite As String
'Choix de civilité
For Each bouton_civilite In Frame_Civilite.Controls
If bouton_civilite.Value Then
civilite = bouton_civilite.Caption
End If
Next
'no_ligne = N° de ligne de la dernière cellule non vide de la colonne +1
no_ligne = Range("A65536").End(xlUp).Row + 1
'Insertion des valeurs sur la feuille
Cells(no_ligne, 1) = civilite
Cells(no_ligne, 2) = TextBox_Nom.Value
Cells(no_ligne, 3) = TextBox_Prenom.Value
Cells(no_ligne, 4) = TextBox_Adresse.Value
Cells(no_ligne, 5) = TextBox_Lieu.Value
Cells(no_ligne, 6) = ComboBox_Pays.Value
If ComboBox_Pays.ListIndex = -1 Then
Sheets("Pays").Range("A" & Sheets("Pays").Range("A65536").End(xlUp).Row + 1) = Me.ComboBox_Pays
Me.ComboBox_Pays.AddItem Me.ComboBox_Pays
End If
'Après insertion, on remet les valeurs initiales
OptionButton1.Value = True
TextBox_Nom.Value = ""
TextBox_Prenom.Value = ""
TextBox_Adresse.Value = ""
TextBox_Lieu.Value = ""
ComboBox_Pays.ListIndex = -1
End If
End Sub
Private Sub UserForm_Initialize() 'Liste des 231 pays de la feuille "Pays"
For i = 1 To Sheets("Pays").Range("A65536").End(xlUp).Row
ComboBox_Pays.AddItem Sheets("Pays").Cells(i, 1)
Next
End Sub
Merci Lone-wolf parfaitBonjour adilprodijy, JM27, le Forum
Ton fichier en retour. Clique sur nouveau pays, inscrit le nom et valide; ensuite ferme le formulaire.
Private Sub CommandButton1_Click()
Dim lig As Long, rw
With Sheets("Pays")
lig = .Cells(Rows.Count, 1).End(xlUp).Row + 1
rw = Application.Match(TextBox1.Value, .Columns(1), 0)
.Cells(lig, 1) = TextBox1.Value
If IsError(rw) Then Exit Sub
'Dans le cas où le pays est déjà inscrit
If .Cells(rw, 1) = TextBox1.Value Then
MsgBox "Ce pays est déjà inscrit.", , "ERREUR"
TextBox1 = ""
End If
End With
TextBox1 = ""
Ajout_contact.ComboBox_Pays.Clear
End Sub
Bonjour
Comme cela ?
Code:Private Sub CommandButton_Ajouter_Click() 'Coloration des Labels en noir 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) 'Contrôles de contenu If TextBox_Nom.Value = "" Then 'SI pas de "nom" ... Label_Nom.ForeColor = RGB(255, 0, 0) 'Label "nom" en rouge ElseIf TextBox_Prenom.Value = "" Then Label_Prenom.ForeColor = RGB(255, 0, 0) ElseIf TextBox_Adresse.Value = "" Then Label_Adresse.ForeColor = RGB(255, 0, 0) ElseIf TextBox_Lieu.Value = "" Then Label_Lieu.ForeColor = RGB(255, 0, 0) ElseIf ComboBox_Pays.Value = "" Then Label_Pays.ForeColor = RGB(255, 0, 0) Else 'Si le formulaire est complet, on insère les valeurs sur la feuille Dim no_ligne As Integer, civilite As String 'Choix de civilité For Each bouton_civilite In Frame_Civilite.Controls If bouton_civilite.Value Then civilite = bouton_civilite.Caption End If Next 'no_ligne = N° de ligne de la dernière cellule non vide de la colonne +1 no_ligne = Range("A65536").End(xlUp).Row + 1 'Insertion des valeurs sur la feuille Cells(no_ligne, 1) = civilite Cells(no_ligne, 2) = TextBox_Nom.Value Cells(no_ligne, 3) = TextBox_Prenom.Value Cells(no_ligne, 4) = TextBox_Adresse.Value Cells(no_ligne, 5) = TextBox_Lieu.Value Cells(no_ligne, 6) = ComboBox_Pays.Value If ComboBox_Pays.ListIndex = -1 Then Sheets("Pays").Range("A" & Sheets("Pays").Range("A65536").End(xlUp).Row + 1) = Me.ComboBox_Pays Me.ComboBox_Pays.AddItem Me.ComboBox_Pays End If 'Après insertion, on remet les valeurs initiales OptionButton1.Value = True TextBox_Nom.Value = "" TextBox_Prenom.Value = "" TextBox_Adresse.Value = "" TextBox_Lieu.Value = "" ComboBox_Pays.ListIndex = -1 End If End Sub
et dans l'initialize
Code:Private Sub UserForm_Initialize() 'Liste des 231 pays de la feuille "Pays" For i = 1 To Sheets("Pays").Range("A65536").End(xlUp).Row ComboBox_Pays.AddItem Sheets("Pays").Cells(i, 1) Next End Sub
Re
Je ne comprend pas ce que tu veux dire?!
Edit: met ce code à la place de l'ancien dans le formulaire pays.
VB:Private Sub CommandButton1_Click() Dim lig As Long, rw With Sheets("Pays") lig = .Cells(Rows.Count, 1).End(xlUp).Row + 1 rw = Application.Match(TextBox1.Value, .Columns(1), 0) .Cells(lig, 1) = TextBox1.Value If IsError(rw) Then Exit Sub 'Dans le cas où le pays est déjà inscrit If .Cells(rw, 1) = TextBox1.Value Then MsgBox "Ce pays est déjà inscrit.", , "ERREUR" TextBox1 = "" End If End With TextBox1 = "" Ajout_contact.ComboBox_Pays.Clear End Sub
Private Sub CommandButton1_Click()
Dim lig As Long, rw
With Sheets("Pays")
lig = .Cells(Rows.Count, 1).End(xlUp).Row + 1
rw = Application.Match(ComboBox_Pays.Value, .Columns(1), 0)
.Cells(lig, 1) = ComboBox_Pays.Value
'Dans le cas où le pays est déjà inscrit
If IsError(rw) Then ComboBox_Pays = "": Exit Sub
If .Cells(rw, 1) = ComboBox_Pays.Value Then
MsgBox "Ce pays est déjà inscrit.", , "ERREUR"
TextBox1 = ""
End If
End With
Ajout_contact.ComboBox_Pays.Clear
End Sub
rw = Application.Match(ComboBox_Pays.Value, .Columns(1), 0)
.Cells(lig, 1) = ComboBox_Pays.Value '<-------ICI
'Dans le cas où le pays est déjà inscrit
If IsError(rw) Then ComboBox_Pays = "": Exit Sub