fredoud
Nouveau membre
Bonjour,
Je suis e train de me faire une application pour gérer la liste de clients et donc je me suis lancer depuis une semaine dans l'userform et vba d'excel, que de grand mot ! Etant novice en la matière, je demande votre concours afin de pouvoir résoudre mon probleme. A l'ouverture de l'userform (bouton "ajout_client" dans feuille "tableau de bord"), je souhaite créer un nouveau client donc je remplie les combobox et textbox qui compose mon userform et je clique sur ajouter. Les donnees devrait etre copier dans la feuille nommé "base" et dans la première ligne vide qu'il trouve. J'ai ajouté dans le code, lorsque je renseigne les combobox si le nom ou autres données n'apparaissent pas dans une des listes de la feuille "Liste", un message me signale si je veux l'ajouter et le tri se fait automatiquement dans chacunes des listes. J'ai aussi la possibilité de faire des modifications des lignes de la feuille base mais je verrai après.
Pouvez-vous me corriger d'eventuels erreurs, s'il vous plait ?
Voici le code en faisant clique droit de mon userform et je selectionne code :
CODE :
Option Explicit
Dim Ligne As Integer
Dim Feuil As Worksheet
Set Feuil = Worksheets("Base")
Ligne = Sheets("Feuil").[A65536].End(xlUp).Offset(1, 0).Row
Private Sub CmdAjouter_Click()
Me.ComboBox_nom = ""
Me.TextBox_pro = ""
Me.CheckBox_Achat = False
Me.ComboBox_annee = ""
Me.ComboBox_mois = ""
Me.TextBox_CA = ""
Me.ComboBox_connais = ""
Me.ComboBox_parrainage = ""
Me.ComboBox_ville = ""
Me.ComboBox_FAI = ""
Me.ComboBox_antivirus = ""
End Sub
Private Sub CmdAnnuler_Click()
Unload Me
End Sub
Private Sub CmdValider_Click()
Sheets("Base").Cells(Ligne, 2) = Application.Proper(Me!ComboBox_nom)
Sheets("Base").Cells(Ligne, 3) = Application.Proper(Me.TextBox_pro)
Sheets("Base").Cells(Ligne, 4) = Me.CheckBox_Achat 'OuiNon(Me.Achat)
Sheets("Base").Cells(Ligne, 5) = Me.ComboBox_annee
Sheets("Base").Cells(Ligne, 6) = Me.ComboBox_mois
Sheets("Base").Cells(Ligne, 7) = Me.TextBox_CA
If TextBox_CA.Text = "" Then
MsgBox "Le C.A. n'est pas indiqué"
Exit Sub
End If
Sheets("Base").Cells(Ligne, 8) = Application.Proper(Me.ComboBox_connais)
Sheets("Base").Cells(Ligne, 9) = Me.ComboBox_parrainage
Sheets("Base").Cells(Ligne, 10) = Application.Proper(Me.ComboBox_ville)
Sheets("Base").Cells(Ligne, 11) = Application.Proper(Me.ComboBox_FAI)
Sheets("Base").Cells(Ligne, 12) = Application.Proper(Me.ComboBox_antivirus)
End Sub
Private Sub ComboBox_nom_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_nom, Range("Listes_clients"), 0)) And Me.ComboBox_nom <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_clients").End(xlDown).Offset(1, 0) = Me.ComboBox_nom
Range("Listes_clients").Sort Key1:=Range("Listes_clients")(1)
End If
End If
End Sub
Private Sub ComboBox_annee_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_annee, Range("Listes_annee"), 0)) And Me.ComboBox_annee <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_annee").End(xlDown).Offset(1, 0) = Me.ComboBox_annee
Range("Listes_annee").Sort Key1:=Range("Listes_annee")(1)
End If
End If
End Sub
Private Sub ComboBox_connais_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_connais, Range("Listes_connaissances"), 0)) And Me.ComboBox_connais <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_connaissances").End(xlDown).Offset(1, 0) = Me.ComboBox_connais
Range("Listes_connaissances").Sort Key1:=Range("Listes_connaissances")(1)
End If
End If
End Sub
Private Sub ComboBox_ville_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_ville, Range("Listes_villes"), 0)) And Me.ComboBox_ville <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_villes").End(xlDown).Offset(1, 0) = Me.ComboBox_ville
Range("Listes_villes").Sort Key1:=Range("Listes_villes")(1)
End If
End If
End Sub
Private Sub ComboBox_FAI_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_FAI, Range("Listes_FAI"), 0)) And Me.ComboBox_FAI <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_FAI").End(xlDown).Offset(1, 0) = Me.ComboBox_FAI
Range("Listes_FAI").Sort Key1:=Range("Listes_FAI")(1)
End If
End If
End Sub
Private Sub ComboBox_antivirus_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_antivirus, Range("Listes_antivirus"), 0)) And Me.ComboBox_antivirus <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_antivirus").End(xlDown).Offset(1, 0) = Me.ComboBox_antivirus
Range("Listes_antivirus").Sort Key1:=Range("Listes_antivirus")(1)
End If
End If
End Sub
Private Sub CheckBox_Achat_Click()
'Si casse cocher, la valeur 1 sera renvoyer sur la ligne active dans la colonne D de la feuille Base
If CheckBox_Achat.Value Then
Range("D").Value = 1
Else
Colunms("D").Value = ""
End If
End Sub
Private Sub UserForm_Activate()
If Range("E3").Value Then
Me.CheckBox_Achat.Value = True
Else: Me.CheckBox_Achat.Value = False
End If
End Sub
Private Sub CmdPremier_Click()
ComboBox1.ListIndex = 0
End Sub
Private Sub ComboBox1_Change()
If ComboBox1.ListIndex = -1 Then Exit Sub
Me.CmdPremier.Enabled = True
Me.CmdPrecedent.Enabled = True
Me.CmdSuivant.Enabled = True
Me.CmdDernier.Enabled = True
Select Case ComboBox1.ListIndex
Case -1
Me.CmdPremier.Enabled = False
Case 0
Me.CmdPrecedent.Enabled = False
Me.CmdPremier.Enabled = False
Case ComboBox1.ListCount - 1
Me.CmdSuivant.Enabled = False
Me.CmdDernier.Enabled = False
End Select
Worksheets("Base").Cells(ComboBox1.ListIndex + 1, 1).Select
Navigue ActiveCell.Row
End Sub
Private Sub CmdPrecedent_Click()
ComboBox1.ListIndex = ComboBox1.ListIndex - 1
End Sub
Private Sub CmdSuivant_Click()
ComboBox1.ListIndex = ComboBox1.ListIndex + 1
End Sub
Private Sub CmdDernier_Click()
ComboBox1.ListIndex = ComboBox1.ListCount - 1
End Sub
Private Sub Navigue(L As Long)
Dim i As Byte
With Worksheets("Base")
For i = 2 To 12
Me("TextBox" & i) = .Cells(L, i)
Next
End With
End Sub
Private Sub UserForm_Initialize()
ComboBox_mois.ListIndex = Month(Now()) - 1
ComboBox_annee = Year(Now)
With Worksheets("Base")
Me.ComboBox1.List = .Range("b2:B" & .Range("A65536").End(xlUp).Row).Value
Me.ComboBox1.ListIndex = ActiveCell.Row - 1
End With
End Sub
En vous remerciant à l'avance,
Frédérique
PS : si vous pouvez etre precis dans les changements a apporter du "mon code remplacer par ..."
Je suis e train de me faire une application pour gérer la liste de clients et donc je me suis lancer depuis une semaine dans l'userform et vba d'excel, que de grand mot ! Etant novice en la matière, je demande votre concours afin de pouvoir résoudre mon probleme. A l'ouverture de l'userform (bouton "ajout_client" dans feuille "tableau de bord"), je souhaite créer un nouveau client donc je remplie les combobox et textbox qui compose mon userform et je clique sur ajouter. Les donnees devrait etre copier dans la feuille nommé "base" et dans la première ligne vide qu'il trouve. J'ai ajouté dans le code, lorsque je renseigne les combobox si le nom ou autres données n'apparaissent pas dans une des listes de la feuille "Liste", un message me signale si je veux l'ajouter et le tri se fait automatiquement dans chacunes des listes. J'ai aussi la possibilité de faire des modifications des lignes de la feuille base mais je verrai après.
Pouvez-vous me corriger d'eventuels erreurs, s'il vous plait ?
Voici le code en faisant clique droit de mon userform et je selectionne code :
CODE :
Option Explicit
Dim Ligne As Integer
Dim Feuil As Worksheet
Set Feuil = Worksheets("Base")
Ligne = Sheets("Feuil").[A65536].End(xlUp).Offset(1, 0).Row
Private Sub CmdAjouter_Click()
Me.ComboBox_nom = ""
Me.TextBox_pro = ""
Me.CheckBox_Achat = False
Me.ComboBox_annee = ""
Me.ComboBox_mois = ""
Me.TextBox_CA = ""
Me.ComboBox_connais = ""
Me.ComboBox_parrainage = ""
Me.ComboBox_ville = ""
Me.ComboBox_FAI = ""
Me.ComboBox_antivirus = ""
End Sub
Private Sub CmdAnnuler_Click()
Unload Me
End Sub
Private Sub CmdValider_Click()
Sheets("Base").Cells(Ligne, 2) = Application.Proper(Me!ComboBox_nom)
Sheets("Base").Cells(Ligne, 3) = Application.Proper(Me.TextBox_pro)
Sheets("Base").Cells(Ligne, 4) = Me.CheckBox_Achat 'OuiNon(Me.Achat)
Sheets("Base").Cells(Ligne, 5) = Me.ComboBox_annee
Sheets("Base").Cells(Ligne, 6) = Me.ComboBox_mois
Sheets("Base").Cells(Ligne, 7) = Me.TextBox_CA
If TextBox_CA.Text = "" Then
MsgBox "Le C.A. n'est pas indiqué"
Exit Sub
End If
Sheets("Base").Cells(Ligne, 8) = Application.Proper(Me.ComboBox_connais)
Sheets("Base").Cells(Ligne, 9) = Me.ComboBox_parrainage
Sheets("Base").Cells(Ligne, 10) = Application.Proper(Me.ComboBox_ville)
Sheets("Base").Cells(Ligne, 11) = Application.Proper(Me.ComboBox_FAI)
Sheets("Base").Cells(Ligne, 12) = Application.Proper(Me.ComboBox_antivirus)
End Sub
Private Sub ComboBox_nom_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_nom, Range("Listes_clients"), 0)) And Me.ComboBox_nom <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_clients").End(xlDown).Offset(1, 0) = Me.ComboBox_nom
Range("Listes_clients").Sort Key1:=Range("Listes_clients")(1)
End If
End If
End Sub
Private Sub ComboBox_annee_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_annee, Range("Listes_annee"), 0)) And Me.ComboBox_annee <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_annee").End(xlDown).Offset(1, 0) = Me.ComboBox_annee
Range("Listes_annee").Sort Key1:=Range("Listes_annee")(1)
End If
End If
End Sub
Private Sub ComboBox_connais_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_connais, Range("Listes_connaissances"), 0)) And Me.ComboBox_connais <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_connaissances").End(xlDown).Offset(1, 0) = Me.ComboBox_connais
Range("Listes_connaissances").Sort Key1:=Range("Listes_connaissances")(1)
End If
End If
End Sub
Private Sub ComboBox_ville_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_ville, Range("Listes_villes"), 0)) And Me.ComboBox_ville <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_villes").End(xlDown).Offset(1, 0) = Me.ComboBox_ville
Range("Listes_villes").Sort Key1:=Range("Listes_villes")(1)
End If
End If
End Sub
Private Sub ComboBox_FAI_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_FAI, Range("Listes_FAI"), 0)) And Me.ComboBox_FAI <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_FAI").End(xlDown).Offset(1, 0) = Me.ComboBox_FAI
Range("Listes_FAI").Sort Key1:=Range("Listes_FAI")(1)
End If
End If
End Sub
Private Sub ComboBox_antivirus_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(Me.ComboBox_antivirus, Range("Listes_antivirus"), 0)) And Me.ComboBox_antivirus <> "" Then
If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
Range("Listes_antivirus").End(xlDown).Offset(1, 0) = Me.ComboBox_antivirus
Range("Listes_antivirus").Sort Key1:=Range("Listes_antivirus")(1)
End If
End If
End Sub
Private Sub CheckBox_Achat_Click()
'Si casse cocher, la valeur 1 sera renvoyer sur la ligne active dans la colonne D de la feuille Base
If CheckBox_Achat.Value Then
Range("D").Value = 1
Else
Colunms("D").Value = ""
End If
End Sub
Private Sub UserForm_Activate()
If Range("E3").Value Then
Me.CheckBox_Achat.Value = True
Else: Me.CheckBox_Achat.Value = False
End If
End Sub
Private Sub CmdPremier_Click()
ComboBox1.ListIndex = 0
End Sub
Private Sub ComboBox1_Change()
If ComboBox1.ListIndex = -1 Then Exit Sub
Me.CmdPremier.Enabled = True
Me.CmdPrecedent.Enabled = True
Me.CmdSuivant.Enabled = True
Me.CmdDernier.Enabled = True
Select Case ComboBox1.ListIndex
Case -1
Me.CmdPremier.Enabled = False
Case 0
Me.CmdPrecedent.Enabled = False
Me.CmdPremier.Enabled = False
Case ComboBox1.ListCount - 1
Me.CmdSuivant.Enabled = False
Me.CmdDernier.Enabled = False
End Select
Worksheets("Base").Cells(ComboBox1.ListIndex + 1, 1).Select
Navigue ActiveCell.Row
End Sub
Private Sub CmdPrecedent_Click()
ComboBox1.ListIndex = ComboBox1.ListIndex - 1
End Sub
Private Sub CmdSuivant_Click()
ComboBox1.ListIndex = ComboBox1.ListIndex + 1
End Sub
Private Sub CmdDernier_Click()
ComboBox1.ListIndex = ComboBox1.ListCount - 1
End Sub
Private Sub Navigue(L As Long)
Dim i As Byte
With Worksheets("Base")
For i = 2 To 12
Me("TextBox" & i) = .Cells(L, i)
Next
End With
End Sub
Private Sub UserForm_Initialize()
ComboBox_mois.ListIndex = Month(Now()) - 1
ComboBox_annee = Year(Now)
With Worksheets("Base")
Me.ComboBox1.List = .Range("b2:B" & .Range("A65536").End(xlUp).Row).Value
Me.ComboBox1.ListIndex = ActiveCell.Row - 1
End With
End Sub
En vous remerciant à l'avance,
Frédérique
PS : si vous pouvez etre precis dans les changements a apporter du "mon code remplacer par ..."