[VBA] Rajouter une fonction modifier avec une fonction de cascade

Bonjour à tous,

J'ai un userform séparé en deux parties : vert qui est codé et qui me rajoute mes lignes dans mon tableau onglet 2.

J'aimerais rajouter dans ma partie Rouge : une première fonction "recherche" avec le premiere filtre LOT dans mon premier combobox, puis qu'apparaisse dans le second combobox le nom et prénom en fonction du numéro de Lot sélectionné.

Et enfin pouvez vous m'aiguiller sur la fonction Modifier qui mettrait à jour les lignes.

Pour résumer cette fonction permettrait de rentrer la ligne "Retour d'emprunt" qui serait une des seules vides.

Merci par avance de votre temps accordé

john91

9tracabilite-mae.zip (472.73 Ko)

Hello,

une première fonction "recherche" avec le premiere filtre LOT dans mon premier combobox

Tu veux que ta combobox contienne une liste de lot sans doublon, ou tu veux taper un num lot et ça t'affiche tous les lots correspondants à ce que tu as inscris ?

Bonsoir Rag02700, merci de prendre de ton temps.

Dans la combobox « numéro de lot » je ne voudrais pas de doublon. Donc au maximum une liste déroulante de 26 lots.

Dans le tableau si « Lot 1 » apparaît plusieurs fois, dans la combobox il doit apparaître qu’une fois.

Cependant, dans le combobox des « noms et prénoms » j’aimerais que ça apparaisse en cascade en fonction du lot sélectionné.

Exemple : Lot 1 apparaît une seule fois dans ma combobox « Lot ». Et les noms et prénoms associés a Lot 1 dans mon tableau apparaissent dans la combobox2

Avec ces deux filtres, j’espère que les informations correspondant à une ligne se remplissent automatiquement afin que par la suite je puisse rajouter ma cellule « date de retour » en cliquant sur « Mettre a jour »

Merci d’avoir pris le temps de me lire et de m’aider.

Hello,

Dans la combobox « numéro de lot » je ne voudrais pas de doublon. Donc au maximum une liste déroulante de 26 lots.

Modifier comme ceci :

Private Sub UserForm_Initialize()
TextBox3.Text = "jj/mm/aaaa"
ComboBox4.RowSource = "=LOT"
End Sub

Cependant, dans le combobox des « noms et prénoms » j’aimerais que ça apparaisse en cascade en fonction du lot sélectionné.

Modifier comme ceci :

Private Sub ComboBox4_Change()
Const str_namewshlot As String = "TRACABILITE M.A.E"
Dim i As Long, lng_lot As Long
Dim wsh_lot As Worksheet

ComboBox7.Clear
Set wsh_lot = Sheets(str_namewshlot)
lng_lot = ComboBox4

With wsh_lot
    i = 1
    Do
        i = i + 1
        If .Cells(i, "A") = lng_lot Then Me.ComboBox7.AddItem .Cells(i, "E")
    Loop While Not .Cells(i, "A") = Empty
End With
Set wsh_lot = Nothing
End Sub

Combobox4 étant la combobox num lot & combobox7 est celle des noms prenoms

Merci milles fois, je vais essayer de ce pas

Edit : J'ai réussi après avoir viré le raw qui s'etait mis sur les combobox! Merci

J'essaye de creuser pour remplir automatiquement avec les données qu'on récupère

Je peux commenter le code si tu veux

De ce que j'ai compris, il faut positionner la cellule où il y aura le nom et prénom voulu avec le ActiveCell?

Edit j'ai essayé :

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ligne As Integer: Dim colonne As Byte

ligne = Target.Row: colonne = Target.Column

If (ligne >= 2 And colonne >= 1 And colonne <= 6) Then

ligneSel = ligne

Sheets("TRACABILITE M.A.E").Range("A" & ligne & "G" & ligne).Select
Range("Combobox5").Value = Sheets("TRACABILITE M.A.E").Range("B" & ligne).Value
Range("Textbox7").Value = Sheets("TRACABILITE M.A.E").Range("C" & ligne).Value
Range("Textbox8").Value = Sheets("TRACABILITE M.A.E").Range("D" & ligne).Value
Range("Textbox9").Value = Sheets("TRACABILITE M.A.E").Range("F" & ligne).Value
Range("Combobox6").Value = Sheets("TRACABILITE M.A.E").Range("G" & ligne).Value

End Sub

Salut

Une autre proposition que je travaille sur depuis plus d'une heure :

Cette proposition tu donne la possibilité d’afficher que les lots mentionner sur la colonne et non pas tous les lots existant ! quand tu ouvre combobox1 tu voix seulement les lots inscrit sur la colonne

Pour supprimer les doublons tu peux utiliser aussi les dictionnaires !

Private Sub UserForm_Initialize()
Set Trsblt = Sheets("TRACABILITE M.A.E")
'ComboBox4.Clear ou 'ComboBox1.Clear
drlgn = Trsblt.Cells(Rows.Count, "A").End(xlUp).Row
For i = drlgn To 2 Step -1
n = Application.CountIf(Trsblt.Range("a" & i & ":a" & drlgn), Trsblt.Cells(i, 1))
If n = 1 Then ComboBox1.AddItem Trsblt.Cells(i, 1)
Next
'TextBox3.Text = "jj/mm/aaaa"
End Sub
Private Sub ComboBox1_Change()
ComboBox2.Clear 'ou textbox qui doit etre un ComboBox
drlgn = Cells(Rows.Count, "A").End(xlUp).Row
For j = 2 To drlgn
If ComboBox1.Text = Trsblt.Cells(j, 1) Then ComboBox2.AddItem Trsblt.Cells(j, "E")
Next
End Sub

Bonsoir Amir et merci pour ton temps accordé

J'ai essayé ton code avec les bons combobox mais je n'ai pas réussi. J'ai essayé en ayant une erreur:

Private Sub UserForm_Initialize()
Set Trsblt = Sheets("TRACABILITE M.A.E")
ComboBox4.Clear
drlgn = Trsblt.Cells(Rows.Count, "A").End(xlUp).Row
For i = drlgn To 2 Step -1
n = Application.CountIf(Trsblt.Range("a" & i & ":a" & drlgn), Trsblt.Cells(i, 1))
If n = 1 Then ComboBox4.AddItem Trsblt.Cells(i, 1)
Next
TextBox3.Text = "jj/mm/aaaa"
End Sub
Private Sub ComboBox7_Change()
ComboBox7.Clear
drlgn = Cells(Rows.Count, "A").End(xlUp).Row
For j = 2 To drlgn
If ComboBox7.Text = Trsblt.Cells(j, 1) Then ComboBox2.AddItem Trsblt.Cells(j, "E")
Next
End Sub

Lot = combobox4

Noms = combobox7

7tracabilite-mae.zip (479.62 Ko)

Essayer cette correction : elle est bien fonctionnelle sur mon ordi

Dim Trsblt ‘ à déclarer en haut de module

'nom&prénom en fonction du num lot'

Private Sub ComboBox4_Change()

ComboBox7.Clear
drlgn = Cells(Rows.Count, "A").End(xlUp).Row
For j = 2 To drlgn
If ComboBox4.Text = Trsblt.Cells(j, 1) Then ComboBox7.AddItem Trsblt.Cells(j, "E")
Next
End Sub

'recherche numero lot'

Private Sub UserForm_Initialize()
Set Trsblt = Sheets("TRACABILITE M.A.E")
ComboBox4.Clear
drlgn = Trsblt.Cells(Rows.Count, "A").End(xlUp).Row
For i = drlgn To 2 Step -1
n = Application.CountIf(Trsblt.Range("a" & i & ":a" & drlgn), Trsblt.Cells(i, 1))
If n = 1 Then ComboBox4.AddItem Trsblt.Cells(i, 1)
Next
TextBox3.Text = "jj/mm/aaaa"
End Sub

Merci pour cette correction qui fonctionne sur mon ordi

Y a-t-il une méthode pour trier du plus petit au plus grand? Car quand ma base de données sera assez conséquente, cela risque d'être compliqué de chercher le bon numéro

Merci

sincèrement j ai pense que tu va me demander de trier cette liste des lots :)

dit moi si les lots sont vraiment et que des chiffre allant de 1 a 26

tu as voulu me devancer sur ma demande!

oui de 1 à 26. Pas plus.

encore merci

essayer bien cette correction :

Private Sub UserForm_Initialize()
Dim tbl, m As Integer
m = 0
ReDim tbl(m)
Set Trsblt = Sheets("TRACABILITE M.A.E")
'ComboBox4.Clear
drlgn = Trsblt.Cells(Rows.Count, "A").End(xlUp).Row
For i = drlgn To 2 Step -1
n = Application.CountIf(Trsblt.Range("a" & i & ":a" & drlgn), Trsblt.Cells(i, 1))
If n = 1 Then
ReDim Preserve tbl(m)
tbl(m) = Val(Trsblt.Cells(i, 1)): m = m + 1
End If
Next
For b = 1 To 26
For j = 0 To m - 1
If tbl(j) = b Then ComboBox4.AddItem b: Exit For
Next
Next

TextBox3.Text = "jj/mm/aaaa"

End Sub

Parfait Amir, merci.

Pour pré-remplir les données j'ai essayé :

Private Sub ComboBox7_Change()
Dim ligne As Long

If ComboBox7.ListIndex = -1 Then Exit Sub
With Ws
ligne = .Columns(2).Find(ComboBox7.Value, LookIn:=xlValues).Row
ligne = ComboBox7.ListIndex + 1
    Controls("Combobox5") = Ws.Cells(ligne, 2)
    Controls("textbox7") = Ws.Cells(ligne, 3)
    Controls("textbox8") = Ws.Cells(ligne, 4)
    Controls("textbox9") = Ws.Cells(ligne, 6)
    Controls("combobox6") = Ws.Cells(ligne, 7)
End With
End Sub

Mais la ligne suivante ne semble pas correcte

ligne = .Columns(2).Find(ComboBox7.Value, LookIn:=xlValues).Row

Salut

Quelque proposition:

Lors de la déclaration de variables, vous employez généralement une instruction Dim.

Déclarer les variables de feuilles comme "Ws" dans la section Déclarations du module, "Ws" est alors accessible dans toutes les procédures du module

puis initialiser cette variable a partir de "UserForm_Initialize" comme ça : 'As Worksheet ….. set Ws=sheets("bla bla").

Bien sur : Si tu suppose que les autres contrôles vont reçoit qu’une entré chacune cette méthode est juste, il suffit seulement de vérifier si tu es sur la bonne ligne. Par exemple tu mets une « Msgbox ligne » comme j ai fait dans le code

Et si tu suppose que les autres contrôles vont reçoit une entrés ou plusieurs chacune cette méthode est fausse

Important : Effacer le RowSource de Combobox5 et Combobox6.

Private Sub ComboBox7_Change()
Dim ligne

If ComboBox7.ListIndex = -1 Then
Exit Sub
'OU : enlever Exit Sub et metre
    'Controls("Combobox5").ListIndex = -1
   ' Controls("textbox7") = ""
   ' Controls("textbox8") = ""
   ' Controls("textbox9") = ""
   ' Controls("combobox6").ListIndex = -1
End If

With Ws
Set ligne = .Columns("E").Find(ComboBox7.Value, LookIn:=xlValues) 'set
'tu cherche ComboBox7.Value dans la colonne 2=B qui est pour "SERVICE DEMANDEUR"
'alors que tu doit le cherche sur la colonne 5 == "E"
'tu peux utiliser cette methode =  .Cells(1, "B")
If Not ligne Is Nothing Then 'ligne = ComboBox7.ListIndex + 1
MsgBox ligne.Address 'tu peux l effacer apres cest juste pour verifier
'effacer le rowsource de Combobox5 et Combobox6
   Controls("Combobox5").AddItem .Cells(ligne.Row, 2)
   Controls("Combobox5").ListIndex = 0
    Controls("textbox7") = .Cells(ligne.Row, 3) '.Cells(1, "Bla bla")
    Controls("textbox8") = .Cells(ligne.Row, 4) '.Cells(1, "C")
    Controls("textbox9") = .Cells(ligne.Row, 6) '.Cells(1, "Bla bla")
    Controls("combobox6").AddItem .Cells(ligne.Row, 7) '.AddItem
     Controls("combobox6").ListIndex = 0
End If
End With
End Sub

Merci Amir je testerai cela demain.

Bonjour amir,

Il me demande un débogage sur cette ligne à partir du moment où je sélectionne le nom dans la combobox7

Set ligne = .Columns("E").Find(ComboBox7.Value, LookIn:=xlValues)
2tracabilite-mae.zip (458.49 Ko)

De ce que je comprends c'est par rapport à Dim ligne.

Private Sub ComboBox7_Change()
Dim ligne

If ComboBox7.ListIndex = -1 Then
Exit Sub
Controls("Combobox5").ListIndex = -1
   Controls("textbox7") = ""
   Controls("textbox8") = ""
   Controls("textbox9") = ""
   Controls("combobox6").ListIndex = -1
End If

With Ws
Set ligne = .Columns("E").Find(ComboBox7.Value, LookIn:=xlValues) 'set
If Not ligne Is Nothing Then ligne = ComboBox7.ListIndex + 1
MsgBox ligne.Address
Controls("Combobox5").AddItem .Cells(ligne.Row, 2)
   Controls("Combobox5").ListIndex = 0
   Controls("textbox7") = .Cells(ligne.Row, 3) '.Cells(1, "Bla bla")
   Controls("textbox8") = .Cells(ligne.Row, 4) '.Cells(1, "C")
   Controls("textbox9") = .Cells(ligne.Row, 6) '.Cells(1, "Bla bla")
   Controls("combobox6").AddItem .Cells(ligne.Row, 7) '.AddItem
   Controls("combobox6").ListIndex = 0

End With
End Sub

Il ne faudrait pas que ça soit un range?

Salut

Important : Effacer la propriété RowSource de Combobox5 et Combobox6.

01

Quelque proposition:

Lors de la déclaration de variables, vous employez généralement une instruction Dim.

Déclarer les variables de feuilles comme "Ws" dans la section Déclarations du module, "Ws" est alors accessible dans toutes les procédures du module :

As Worksheet …..

'OU Dim Trsblt 'à déclarer en haut de module
Dim Ws 'à déclarer en haut de module

puis initialiser cette variable a partir de "UserForm_Initialize" comme ça : 'set Ws=sheets("bla bla").

Private Sub UserForm_Initialize()
Dim tbl, m As Integer
m = 0
ReDim tbl(m)
'OU Set Trsblt = Sheets("TRACABILITE M.A.E")
Set Ws = Sheets("TRACABILITE M.A.E")

c’est le code complet de userform1

'OU Dim Trsblt 'à déclarer en haut de module
Dim Ws 'à déclarer en haut de module

'nom&prénom en fonction du num lot'

Private Sub ComboBox4_Change()
ComboBox7.Clear
drlgn = Cells(Rows.Count, "A").End(xlUp).Row
For j = 2 To drlgn
If ComboBox4.Text = Ws.Cells(j, 1) Then ComboBox7.AddItem Ws.Cells(j, "E")
Next
End Sub

'recherche numero lot'

Private Sub UserForm_Initialize()
Dim tbl, m As Integer
m = 0
ReDim tbl(m)
'OU Set Trsblt = Sheets("TRACABILITE M.A.E")
Set Ws = Sheets("TRACABILITE M.A.E")
ComboBox4.Clear
drlgn = Ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = drlgn To 2 Step -1
n = Application.CountIf(Ws.Range("a" & i & ":a" & drlgn), Ws.Cells(i, 1))
If n = 1 Then
ReDim Preserve tbl(m)
tbl(m) = Val(Ws.Cells(i, 1)): m = m + 1
End If
Next
For b = 1 To 26
For j = 0 To m - 1
If tbl(j) = b Then ComboBox4.AddItem b: Exit For
Next
Next

TextBox3.Text = "jj/mm/aaaa"

End Sub

Private Sub ComboBox7_Change()
Dim ligne As Range

If ComboBox7.ListIndex = -1 Then
Exit Sub
Controls("Combobox5").ListIndex = -1
   Controls("textbox7") = ""
   Controls("textbox8") = ""
   Controls("textbox9") = ""
   Controls("combobox6").ListIndex = -1
End If

With Ws
Set ligne = .Columns("E").Find(ComboBox7.Value, LookIn:=xlValues) 'set
'tu cherche ComboBox7.Value dans la colonne 2=B qui est pour "SERVICE DEMANDEUR"
'alors que tu doit le cherche sur la colonne 5 == "E"
'tu peux utiliser cette methode =  .Cells(1, "B")
If Not ligne Is Nothing Then ligne = ComboBox7.ListIndex + 1
MsgBox ligne.Address 'tu peux l effacer apres cest juste pour verifier
'effacer le rowsource de Combobox5 et Combobox6
ComboBox5.Clear
ComboBox6.Clear
Controls("Combobox5").AddItem .Cells(ligne.Row, 2)
   Controls("Combobox5").ListIndex = 0
   Controls("textbox7") = .Cells(ligne.Row, 3) '.Cells(1, "Bla bla")
   Controls("textbox8") = .Cells(ligne.Row, 4) '.Cells(1, "C")
   Controls("textbox9") = .Cells(ligne.Row, 6) '.Cells(1, "Bla bla")
   Controls("combobox6").AddItem .Cells(ligne.Row, 7) '.AddItem
   Controls("combobox6").ListIndex = 0

End With
End Sub

Private Sub CommandButton3_Click()
Unload Me
End Sub

Private Sub CommandButton1_Click()
If ComboBox3 = "" Or ComboBox1 = "" Or TextBox3 = "" Or TextBox3 = "jj/mm/aaaa" Or TextBox4 = "" Then
MsgBox ("Il manque une information obligatoire")
Else
    If Sheets("TRACABILITE M.A.E").Range("a2") = "" Then
    Sheets("TRACABILITE M.A.E").Range("a2") = TextBox1
    Else
    Sheets("TRACABILITE M.A.E").ListObjects(1).ListRows.Add
    End If

'dlt fin de tableau a1048576 cellule bas max puis remonter derniere cellule pleine'
        dlt = Sheets("TRACABILITE M.A.E").Range("a1048576").End(xlUp).Row
        Sheets("TRACABILITE M.A.E").Range("a" & dlt) = ComboBox3
        Sheets("TRACABILITE M.A.E").Range("b" & dlt) = ComboBox1
        TextBox3 = Format(TextBox3, "mm/dd/yyyy")
        Sheets("TRACABILITE M.A.E").Range("c" & dlt) = TextBox3.Value
        TextBox3 = Format(TextBox3, "mm/dd/yyyy")
        Sheets("TRACABILITE M.A.E").Range("e" & dlt) = TextBox4
        Sheets("TRACABILITE M.A.E").Range("f" & dlt) = TextBox5
        Sheets("TRACABILITE M.A.E").Range("g" & dlt) = ComboBox2

    Unload UserForm1

End If
End Sub

Private Sub TextBox3_AfterUpdate()
On Error GoTo messagerreur
TextBox3 = Format(TextBox3, "short date")
Exit Sub
messagerreur:
    MsgBox ("Le format n'est pas valide, le format de date est Jour/Mois/Année.")
    TextBox3 = Empty
End Sub

Private Sub TextBox3_Enter()
If TextBox3 = "jj/mm/aaaa" Then
TextBox3 = ""
End If
End Sub

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox3 = "" Then
TextBox3 = "jj/mm/aaaa"
End If
End Sub

Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not ((KeyAscii > 46 And KeyAscii < 58)) Then
KeyAscii = 0
End If
End Sub
Rechercher des sujets similaires à "vba rajouter fonction modifier cascade"