[VBA] Combobox qui ne fonctionne pas

ralph45

XLDnaute Impliqué
Bonjour @tou.te.s !

J'ai copié sur le site de JBoisgontier un code qui doit générer un ComboBox où les premières lettres saisies doivent afficher une liste d'afférences (AutoCompletion).

Seulement, en l'adaptant un peu (référence de la base),le code me génère une erreur...
"Erreur de compilation
Type défini par l'utilisateur non défini"

Voici le code et là où cela plante...
Merci de votre aide !!

Dim a()
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect([A2:A45], Target) Is Nothing And Target.Count = 1 Then
a = Sheets("BASE1").Range("BASE1_VOIE_NOM").Value
Me.ComboBox1.List = a
Me.ComboBox1.Height = Target.Height + 3
Me.ComboBox1.Width = Target.Width
Me.ComboBox1.Top = Target.Top
Me.ComboBox1.Left = Target.Left
Me.ComboBox1 = Target
Me.ComboBox1.Visible = True
Me.ComboBox1.Activate
Else
Me.ComboBox1.Visible = False
End If
End Sub
Private Sub ComboBox1_Change()
ActiveCell.Value = Me.ComboBox1
End Sub
Private Sub ComboBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.ComboBox1.List = a
Me.ComboBox1.Activate
Me.ComboBox1.DropDown
End Sub
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then ActiveCell.Offset(1).Select
End Sub
 

ralph45

XLDnaute Impliqué
Bonjour @tou.te.s !

J'ai trouvé la solution : ce n'était pas une erreur de code, mais en tant que satané blaireau que je suis, j'avais supprimé ma ComboBox1 ! o_O

Le code est donc nickel mais j'aurai besoin de l'adapter à mon fichier (voir en PJ) :
Intégrer dans une même feuille 2 ComboBox, faisant référence à 2 plages nommées et qui impactent 4 colonnes...
ComboBox1 = colonnes A et C
ComboBox2 = Colonnes B et D
Je pense avoir tout saisi dans ma demande dans le fichier joint

Et encore merci de vos lumières !
 

Pièces jointes

  • Listes_Deroulantes_Intuitives.xlsm
    23.2 KB · Affichages: 21

youky(BJ)

XLDnaute Barbatruc
Bonjour,
Essayer comme ceci, j'ai supprimé certaines parties du code, je pense qu'il n'était plus utile.
Bruno
VB:
Dim a(), f
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Set f = Sheets("BASE")
  If Not Intersect(Range("A3:A30,C3:C30"), Target) Is Nothing And Target.Count = 1 Then
    a = Application.Transpose(f.Range("A2:A" & f.[A65000].End(xlUp).Row))
    Me.ComboBox1.List = a
    Me.ComboBox1.Height = Target.Height + 3
    Me.ComboBox1.Width = Target.Width
    Me.ComboBox1.Top = Target.Top
    Me.ComboBox1.Left = Target.Left
    Me.ComboBox1 = Target
    Me.ComboBox1.Visible = True
    Me.ComboBox1.Activate
  Else
    Me.ComboBox1.Visible = False
  End If
  If Not Intersect(Range("B3:B30,D3:D30"), Target) Is Nothing And Target.Count = 1 Then
    a = Application.Transpose(f.Range("C2:C" & f.[C65000].End(xlUp).Row))
    Me.ComboBox2.List = a
    Me.ComboBox2.Height = Target.Height + 3
    Me.ComboBox2.Width = Target.Width
    Me.ComboBox2.Top = Target.Top
    Me.ComboBox2.Left = Target.Left
    Me.ComboBox2 = Target
    Me.ComboBox2.Visible = True
    Me.ComboBox2.Activate
  Else
    Me.ComboBox2.Visible = False
  End If
 
End Sub
Private Sub ComboBox1_Change()
  ActiveCell.Value = Me.ComboBox1
End Sub
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then
    ActiveCell.Offset(1).Select
  End If
End Sub
Private Sub ComboBox2_Change()
  ActiveCell.Value = Me.ComboBox2
End Sub
 

Discussions similaires

Réponses
6
Affichages
227
Réponses
4
Affichages
506

Statistiques des forums

Discussions
312 089
Messages
2 085 206
Membres
102 820
dernier inscrit
SIEG68