Alimenter une liste via combobox

  • Initiateur de la discussion Initiateur de la discussion cibleo
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

cibleo

XLDnaute Impliqué
Bonsoir le forum,

Dans l'Userform2 du fichier joint, j'aimerais intégrer dans le Private Sub CommandButton1_Click() 'Créer la course un bloc d'instructions me permettant d'alimenter ma liste de noms figurant dans la feuille "BaseClients".

Cette liste (nommée "Clients") serait donc incrémenter via la "ComboClients" au moment du clic sur le bouton "Créer la course".

J'ai trouvé dans le fil ci-dessous, l'exemple de Thierry qui répond à ma demande.
Ici la liste est incrémentée sans doublons.

J'ai donc voulu implanté le bloc d'instructions de Thierry (en marron ci-dessous) dans la routine initiale, mais cela ne fonctionne pas 😱

Code:
Private Sub CommandButton1_Click() 'Créer la course
Dim Ctrl As Control, i As Integer
[COLOR=sienna]Dim Plage As Range, Cell As Range[/COLOR]
[COLOR=sienna]Dim L As Integer[/COLOR]
[COLOR=sienna]Dim NewSupplier As String[/COLOR]
[COLOR=sienna]Dim Duplicate As Boolean[/COLOR]
[COLOR=sienna]NewSupplier = Me.ComboClients.Value[/COLOR]
 
[COLOR=sienna]With ThisWorkbook.Worksheets("BaseClients")[/COLOR]
[COLOR=sienna]L = .Range("A65536").End(xlUp).Row + 1[/COLOR]
[COLOR=sienna]Set Plage = .Range(.Range("A2"), .Range("A65536").End(xlUp))[/COLOR]
[COLOR=sienna]For Each Cell In Plage[/COLOR]
[COLOR=sienna]If CStr(NewSupplier) = CStr(Cell.Text) Then[/COLOR]
[COLOR=sienna]Duplicate = True[/COLOR]
[COLOR=sienna]Exit For[/COLOR]
[COLOR=sienna]End If[/COLOR]
[COLOR=sienna]Next[/COLOR]
[COLOR=sienna]If Not Duplicate Then[/COLOR]
[COLOR=sienna].Range("A" & L) = NewSupplier[/COLOR]
[COLOR=sienna]End If[/COLOR]
[COLOR=sienna]End With[/COLOR]
If Me.ComboPCharge.ListIndex = -1 Then
     MsgBox "Choisir un point de départ."
     Exit Sub
  End If
.../...

Pour Info, la routine initiale a été créée par bqtr.

Ci dessous, le lien dans lequel figure le fichier de Thierry qui répond exactement à ma demande.

https://www.excel-downloads.com/threads/pb-avec-source-dune-combobox.54019/

Pouvez-vous me venir en aide une nouvelle fois ?

Bonne soirée à tous.

Cibleo
 

Pièces jointes

Re : Alimenter une liste via combobox

bonjour Cibleo
de cette manière c'est ok
Private Sub CommandButton1_Click() 'Créer la course
Dim Ctrl As Control, i As Integer
Dim Plage As Range, Cell As Range
Dim L As Integer
Dim NewSupplier As String
Dim Duplicate As Boolean

If Me.ComboPCharge.ListIndex = -1 Then
MsgBox "Choisir un point de départ."
Exit Sub
End If

If Me.ComboCivilite.ListIndex = -1 And Me.ComboClients.ListIndex >= 0 Then
MsgBox "Sélectionner une civilité."
Exit Sub
End If


NewSupplier = Me.ComboClients.Value

With ThisWorkbook.Worksheets("BaseClients")
L = .Range("A65536").End(xlUp).Row + 1
Set Plage = .Range(.Range("A2"), .Range("A65536").End(xlUp))
For Each Cell In Plage
If CStr(NewSupplier) = CStr(Cell.Text) Then
Duplicate = True
Exit For
End If
Next
If Not Duplicate Then
.Range("A" & L) = NewSupplier
End If
End With





If Me.ComboDestination.ListIndex = -1 Then
MsgBox "Sélectionner une destination."
Exit Sub
End If

If Me.txtHeure = "" Then
MsgBox "Sélectionner l'heure de départ."
Exit Sub
End If

If Me.txtHeure2 = "" Then
MsgBox "Sélectionner l'heure d'arrivée."
Exit Sub
End If

If Me.txtNbrePers = "" And Me.ComboClients.ListIndex = -1 Then
MsgBox "Sélectionner un client ou un nombre de personne.", vbCritical, "Attention :"
Exit Sub
End If

If ListBox1.ListCount > 0 Then Me.ListBox1.AddItem ""
Me.ListBox1.AddItem txtHeure & " " & Me.ComboPCharge
If Me.ComboCivilite.ListIndex >= 0 And Me.ComboClients.ListIndex >= 0 Then Me.ListBox1.AddItem ComboCivilite & " " & Me.ComboClients
If Me.txtNbrePers <> "" Then Me.ListBox1.AddItem Me.txtNbrePers
If Me.txtTel <> "" Then Me.ListBox1.AddItem Me.txtTel
If Me.txtAdresse <> "" Then Me.ListBox1.AddItem Me.txtAdresse

For Each Ctrl In Me.Controls
If Ctrl.Tag = "L" Then
For i = 0 To Controls(Ctrl.Name).ListCount - 1
If Controls(Ctrl.Name).Selected(i) Then Me.ListBox1.AddItem Controls(Ctrl.Name).List(i)
Next
End If
Next

Me.ListBox1.AddItem txtHeure2 & " " & Me.ComboDestination



If MsgBox("Dans cette tranche horaire, voulez-vous créer une autre course ? ", vbYesNo + vbExclamation, "Attention :") = vbYes Then
Remise_Zero
Me.txtHeure.SetFocus
Else
Remise_Zero
Me.B_Valider.SetFocus
End If

End Sub

à bientôt
 
Re : Alimenter une liste via combobox

Bonjour à tous,
Bonjour bebere et merci de ta réponse,

Reprenons :

J'ai fait ce que tu m'as dit.

Puis j'ai rajouté cela dans le code de l'Userform2

Code:
Private Sub ComboClients_Change()
Me.ComboCivilite.Enabled = True
End Sub

Puis ci-dessous, j'ai rempacé le = -1 par >= 0

parce que la saisie manuelle (je ne parle pas de sélection) d'un nom dans la "ComboClients" entrainait l'apparition du message lorsque je cliquais sur le bouton "Créer la course", je ne pouvais plus continuer la saisie.

Code:
.../...
If Me.txtNbrePers = "" And Me.ComboClients.ListIndex [COLOR=red][B]>= 0[/B][/COLOR] Then
     MsgBox "Sélectionner un client ou un nombre de personne.", vbCritical, "Attention :"
     Exit Sub
  End If
.../...

La saisie effectuée, je clique sur le bouton "Créer la course" :

Le nouveau nom saisi dans la "ComboClients" apparait bien dans ma liste située dans la base clients 🙂

Mais il y un problème, la Listbox1 ne laisse plus apparaitre l'item concernant la "civilité et le nom du nouveau client".

Par contre si je sélectionne un nom déjà présent dans la "ComboClients", cela apparaît bien dans la Listbox1 comme initialement.
Code:
.../...
If ListBox1.ListCount > 0 Then Me.ListBox1.AddItem ""
  Me.ListBox1.AddItem txtHeure & " " & Me.ComboPCharge
  [COLOR=darkred]If Me.ComboCivilite.ListIndex >= 0 And Me.ComboClients.ListIndex >= 0 Then Me.ListBox1.AddItem ComboCivilite & " " & Me.ComboClients[/COLOR]
  If Me.txtNbrePers <> "" Then Me.ListBox1.AddItem Me.txtNbrePers
  If Me.txtTel <> "" Then Me.ListBox1.AddItem Me.txtTel
  If Me.txtAdresse <> "" Then Me.ListBox1.AddItem Me.txtAdresse
.../...

Dois-je modifier la ligne en rouge ci-dessus ou bien y a t-il un problème avec ListIndex.

Pouvez-vous m'aiguiller ?

Bonne soirée Cibleo
 
Re : Alimenter une liste via combobox

bonjour Cibleo
question: pourquoi le bouton gérer les pièces jointes ne fonctionnent pas
voir code bouton et combobox
tu peux entrer nom et prénom en minuscule


Private Sub ComboClients_Change()

If ComboClients.ListIndex = -1 And ComboClients <> "" Then
Me.ComboCivilite.Enabled = True
Else: Me.ComboCivilite.Enabled = False
End If
If ComboClients.ListIndex > -1 Then _
Me.ComboCivilite.Enabled = True

End Sub

une partie du code bouton

NewSupplier = Me.ComboClients.Value

With ThisWorkbook.Worksheets("BaseClients")
L = .Range("A65536").End(xlUp).Row + 1
Set Plage = .Range(.Range("A2"), .Range("A65536").End(xlUp))
For Each Cell In Plage
If CStr(NewSupplier) = CStr(Cell.Text) Then
Duplicate = True
Exit For
End If
Next
If Not Duplicate Then
.Range("A" & L) = UCase(Mid(NewSupplier, 1, InStr(NewSupplier, " ") - 1)) & " " & Application.Proper(Mid(NewSupplier, InStr(NewSupplier, " ") + 1))
.Range("A1").Sort Key1:=.Columns("A"), Header:=xlGuess
Me.ComboClients.Clear
Me.ComboClients.List = .Range(.Range("A2"), .Range("A65536").End(xlUp)).Value
Me.ComboClients.Value = NewSupplier
End If
End With


à bientôt
 
Re : Alimenter une liste via combobox

Bonsoir à tous,
Bonsoir bebere,

Ne pouvant momentanément joindre mon nouveau fichier, je vous transmets les modifications apportées.

En rouge, ce que j'ai rajouté dans le code de l'Userform2

Code:
[COLOR=darkred]Private Sub ComboClients_Change()[/COLOR]
[COLOR=darkred]Me.ComboCivilite.Enabled = True[/COLOR]
[COLOR=darkred]End Sub[/COLOR]

Code:
Private Sub CommandButton1_Click() 'Créer la course
Dim Ctrl As Control, i As Integer
[COLOR=darkred]Dim Plage As Range, Cell As Range[/COLOR]
[COLOR=darkred]Dim L As Integer[/COLOR]
[COLOR=darkred]Dim NewSupplier As String[/COLOR]
[COLOR=darkred]Dim Duplicate As Boolean[/COLOR]
[COLOR=darkred]NewSupplier = Me.ComboClients.Value[/COLOR]
 
[COLOR=darkred]With ThisWorkbook.Worksheets("BaseClients")[/COLOR]
[COLOR=darkred]L = .Range("A65536").End(xlUp).Row + 1[/COLOR]
[COLOR=darkred]Set Plage = .Range(.Range("A2"), .Range("A65536").End(xlUp))[/COLOR]
[COLOR=darkred]For Each Cell In Plage[/COLOR]
[COLOR=darkred]If CStr(NewSupplier) = CStr(Cell.Text) Then[/COLOR]
[COLOR=darkred]Duplicate = True[/COLOR]
[COLOR=darkred]Exit For[/COLOR]
[COLOR=darkred]End If[/COLOR]
[COLOR=darkred]Next[/COLOR]
[COLOR=darkred]If Not Duplicate Then[/COLOR]
[COLOR=darkred][COLOR=green].Range("A" & L) = NewSupplier[/COLOR]
[/COLOR][COLOR=darkred].Range("A1").Sort Key1:=.Columns("A"), Header:=xlGuess[/COLOR]
[COLOR=darkred]Me.ComboClients.Clear[/COLOR]
[COLOR=darkred]Me.ComboClients.List = .Range(.Range("A2"), .Range("A65536").End(xlUp)).Value[/COLOR]
[COLOR=darkred]Me.ComboClients.Value = NewSupplier[/COLOR]
[COLOR=darkred]End If[/COLOR]
[COLOR=darkred]End With[/COLOR]
 
  If Me.ComboPCharge.ListIndex = -1 Then
     MsgBox "Choisir un point de départ."
     Exit Sub
  End If
.../...

En bleu, ce que j'ai modifié :

Code:
.../...
If Me.ComboCivilite.ListIndex = -1 [COLOR=blue]And Me.ComboClients <> ""[/COLOR] Then
     MsgBox "Sélectionner une civilité."
     Exit Sub
  End If
.../...

Code:
.../...
If Me.txtNbrePers = "" [COLOR=blue]And Me.ComboClients = ""[/COLOR] Then
     MsgBox "Sélectionner un client ou un nombre de personne.", vbCritical, "Attention :"
     Exit Sub
  End If
.../...

Code:
.../...
If ListBox1.ListCount > 0 Then Me.ListBox1.AddItem ""
  Me.ListBox1.AddItem txtHeure & " " & Me.ComboPCharge
  If Me.ComboCivilite.ListIndex >= 0 And Me.ComboClients.ListIndex [COLOR=blue]>= -1[/COLOR] Then Me.ListBox1.AddItem ComboCivilite & " " & Me.ComboClients
  If Me.txtNbrePers <> "" Then Me.ListBox1.AddItem Me.txtNbrePers
.../...

Par contre la ligne en vert ci-dessous m'affiche un message d'erreur d'exécution 5.
(Argument ou appel de procédure incorrect)

A quoi sert cette instruction ?

Code:
.../...
If Not Duplicate Then
[COLOR=green].Range("A" & L) = UCase(Mid(NewSupplier, 1, InStr(NewSupplier, " ") - 1)) & " " & Application.Proper(Mid(NewSupplier, InStr(NewSupplier, " ") + 1))[/COLOR]
.Range("A1").Sort Key1:=.Columns("A"), Header:=xlGuess
.../...

Je l'ai donc remplacée par l'instruction initiale et je n'ai plus d'erreur.

Sinon tout a l'air de fonctionner comme je l'espérais avec en bonus le tri par ordre alphabétique 🙂

A+ Cibleo

PS : je n'ai pas pris le temps de regarder l'évènement "Change" de la ComboClients que tu m'as transmis.

Avec toutes les modifications apportées, doit-il remplacer les 2 évènements de la ComboClients déjà mis en place ou cela est-il devenu inutile ?

Bonne soirée à tous
 
Re : Alimenter une liste via combobox

Cibleo

regarde si tu n'as pas une référence MANQUANT dans outils,références si oui tu décoches
si tu entres dans comboboxclient pom api tu auras POM Api
.Range("A" & L) = UCase(Mid(NewSupplier, 1, InStr(NewSupplier, " ") - 1)) & " " & Application.Proper(Mid(NewSupplier, InStr(NewSupplier, " ") + 1))

dans le fichier j'ai changé l'évènement click par change

à bientôt
 
Re : Alimenter une liste via combobox

Bonsoir à tous,
Bonsoir bebere,

L'erreur provenait de ma saisie.

Si je saisis "Dupont jean" cela marche, si je saisis "dupont" cela active l'erreur.

Puis-je saisir l'un ou l'autre sans que cela m'affiche cette erreur.

A défaut, je peux me contenter de ne saisir que le nom converti en majuscule.

A+ Cibleo
 
Re : Alimenter une liste via combobox

bonsoir Cibleo
tu fais un test sur l' espace comme suit

pos = 0
pos = InStr(NewSupplier, " ")
If pos > 0 Then
.Range("A" & L) = UCase(Mid(NewSupplier, 1, InStr(NewSupplier, " ") - 1)) & " " & Application.Proper(Mid(NewSupplier, InStr(NewSupplier, " ") + 1))
Else: .Range("A" & L) = UCase(NewSupplier)
End If

à bientôt
 
Re : Alimenter une liste via combobox

Re bebere,

J'ai testé et cela fonctionne 🙂

Mais il y a un autre petit souci de conversion.

La conversion en majuscules s'opère bien dans ma liste, mais la donnée saisie dans la ComboClients reste en minuscules.

Conséquence, le nom du client apparait en minuscules lors de la validation de ma course dans la Listbox1,

Ne pourrait-on pas opérer d'une manière différente.

A savoir : intégrer la Conversion en majuscule dans l'événement "Exit" de la ComboClients.

Et garder l'instruction en vert ci-dessous.

Code:
.../...
[COLOR=#8b0000]If Not Duplicate Then[/COLOR]
[COLOR=darkred][COLOR=green].Range("A" & L) = NewSupplier[/COLOR]
[/COLOR][COLOR=darkred].Range("A1").Sort Key1:=.Columns("A"), Header:=xlGuess[/COLOR]
[COLOR=darkred]Me.ComboClients.Clear[/COLOR]
[COLOR=black].../...[/COLOR]

J'ai mis ce code événementiel dans la ComboClients, mais il faudrait que je reprennes ton exemple précédent pour obtenir le même résultat.

Code:
Private Sub ComboClients_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.ComboClients = UCase(ComboClients)
End Sub

Qu'en penses-tu ?

Cibleo
 
Re : Alimenter une liste via combobox

bonjour Cibleo
tu as raison,comme suit

Private Sub ComboClients_AfterUpdate()
Dim Pos As Byte

Pos = 0
Pos = InStr(Me.ComboClients.Value, " ")
If Pos > 0 Then
Me.ComboClients.Value = UCase(Mid(Me.ComboClients.Value, 1, _
InStr(Me.ComboClients.Value, " ") - 1)) & " " & _
Application.Proper(Mid(Me.ComboClients.Value, _
InStr(Me.ComboClients.Value, " ") + 1))
Else: Me.ComboClients.Value = UCase(Me.ComboClients.Value)
End If

une partie du code bouton

NewSupplier = Me.ComboClients.Value

With ThisWorkbook.Worksheets("BaseClients")
L = .Range("A65536").End(xlUp).Row + 1
Set Plage = .Range(.Range("A2"), .Range("A65536").End(xlUp))
For Each Cell In Plage
If CStr(NewSupplier) = CStr(Cell.Text) Then
Duplicate = True
Exit For
End If

Next
If Not Duplicate Then
.Range("A" & L) = NewSupplier
.Range("A1").Sort Key1:=.Columns("A"), Header:=xlGuess
Me.ComboClients.Clear
Me.ComboClients.List = .Range(.Range("A2"), .Range("A65536").End(xlUp)).Value
Me.ComboClients.Value = NewSupplier
End If
End With


End Sub

à bientôt
 
Re : Alimenter une liste via combobox

Bonsoir à tous,
Bonsoir bebere,

Voilà, j'ai apporté toutes les modifications ci-dessus dans le fichier joint, c'est parfait 🙂

Mais j'ai un dernier petit problème à régler qui n'a rien à voir avec le titre du post.

Dans le module 11, j'ai intégré une macro (trouvée sur le forum) pour ajuster la hauteur de lignes.

Mais elle n'a pas l'air de bien fonctionner sur ma feuille "Planning".
La plage à prendre en compte est (B4:M15).

Ainsi la hauteur de la ligne 5 devrait être conditionnée par E5,
la ligne 4 par C4 et la ligne 9 par F9 mais cela ne s'ajuste pas correctement !

Je m'y perds dans les arguments de la méthode FIND.

Pouvez-vous y jeter un oeil ?

Bonne soirée Cibleo
 

Pièces jointes

Re : Alimenter une liste via combobox

bonjour Cibleo
Sub Ajuste_Hauteur_ligne()
Dim derligne As Integer
Dim i As Long
'derligne = Range("a65536").End(xlUp).Row
derligne = ActiveSheet.Cells.Find(what:="*", after:=[B1], searchorder:=xlByRows, searchdirection:=xlPrevious).Row
' ActiveSheet.Rows("04:" & derligne).AutoFit
For i = 4 To derligne
ActiveSheet.Rows(i).RowHeight = ActiveSheet.Rows(i).RowHeight * 2
Next

End Sub
à bientôt
 
Re : Alimenter une liste via combobox

Bonjour à tous,
Bonjour bebere,

Lors de ton dernier post, je rédigeais ceci :

J'ai placé ce code avec les modifications en rouge.
Pour la hauteur de ligne, ça fonctionne, j'obtiens le résultat escompté.

Code:
Sub Ajuste_Hauteur_ligne()
Dim derligne As Integer
Dim i As Long
 
[COLOR=red]Columns("c:m").AutoFit[/COLOR]
 
derligne = ActiveSheet.Cells.Find(what:="*", after:=[[COLOR=red]B4[/COLOR]], searchorder:=[COLOR=red]xlByColumns[/COLOR], searchdirection:=xlPrevious).Row
ActiveSheet.Rows("04:" & derligne).AutoFit
 
  For i = 4 To derligne
    ActiveSheet.Rows(i).RowHeight = ActiveSheet.Rows(i).RowHeight + 20
  Next
 
End Sub

Par contre, au niveau de la largeur de colonne, j'ai un petit problème.

J'illustre :

Je règle manuellement la largeur des colonnes (C:M) à 40, j'exécute la macro tout s'ajuste correctement largeur de colonnes comme hauteur de lignes.
Par contre si je règle la largeur à 10, l'ajustement de la largeur des colonnes se fait mal.

Y a t-il une explication ?

Bon dimanche Cibleo
 
Re : Alimenter une liste via combobox

Bonsoir le forum,

J'ai déplacé l'instruction en rouge ci-dessous.

Le résultat escompté est obtenu en éxécutant 2 fois de suite la macro.

C'est déjà mieux 🙄

Avec le code présenté auparavant, il fallait l'exécuter plusieurs fois pour obtenir le résultat escompté.

Code:
Sub Ajuste_Hauteur_ligne()
Dim derligne As Integer
Dim i As Long
 
derligne = ActiveSheet.Cells.Find(what:="*", after:=[B4], searchorder:=xlByColumns, searchdirection:=xlPrevious).Row
ActiveSheet.Rows("04:" & derligne).AutoFit
 
  For i = 4 To derligne
    ActiveSheet.Rows(i).RowHeight = ActiveSheet.Rows(i).RowHeight + 20
    [COLOR=red]Columns("c:m").AutoFit[/COLOR]
  Next
 
End Sub

Va comprendre quelque chose 🙁

Si vous détenez la clé de l'enigme, n'hésitez pas.

Bonne soirée à tous Cibleo
 
Dernière édition:
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
3
Affichages
194
Réponses
3
Affichages
665
Réponses
3
Affichages
452
Retour