IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Récupérer la valeur d'une cellule à partir d'une ComboBox


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    dessinateur
    Inscrit en
    Février 2013
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : dessinateur
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2013
    Messages : 27
    Par défaut Récupérer la valeur d'une cellule à partir d'une ComboBox
    Bonjour,
    A partir d’un formulaire avec 4 ComboBox
    J’aimerai inscrire dans un Label la valeur de la colonne juste à droite du choix de la ComboBox 4

    Cb1 Cb2 Cb3 Cb4 Label
    A A1 A11 A111 TYPE A111
    A A1 A12 A112 TYPE A112
    B B1 B11 A112 TYPE A112

    Merci de votre aide

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    Dim f, a()
     
    Private Sub UserForm_Initialize()
      Set f = Sheets("Cv_Tapis")
      Set mondico = CreateObject("Scripting.Dictionary")
      a = f.Range("A3:E" & f.[A65000].End(xlUp).Row).Value
      For i = LBound(a, 1) To UBound(a, 1)
        mondico(a(i, 1)) = ""
      Next i
      Me.ComboBox1.List = mondico.keys
    End Sub
     
    Private Sub ComboBox1_click()
      Me.ComboBox2.Clear
      Me.ComboBox3.Clear
      Me.ComboBox4.Clear
      Set mondico = CreateObject("Scripting.Dictionary")
      For i = LBound(a, 1) To UBound(a, 1)
         If a(i, 1) = Me.ComboBox1 Then mondico(a(i, 2)) = ""
      Next i
      Me.ComboBox2.List = mondico.keys
      'Me.TextBox1 = Me.ComboBox1
    End Sub
     
    Private Sub ComboBox2_click()
      Me.ComboBox3.Clear
      Me.ComboBox4.Clear
      Set mondico = CreateObject("Scripting.Dictionary")
      For i = LBound(a, 1) To UBound(a, 1)
         If a(i, 1) = Me.ComboBox1 And a(i, 2) = Me.ComboBox2 Then mondico(a(i, 3)) = ""
      Next i
      Me.ComboBox3.List = mondico.keys
      'Me.TextBox1 = Me.ComboBox2
    End Sub
     
    Private Sub ComboBox3_click()
      Me.ComboBox4.Clear
      Set mondico = CreateObject("Scripting.Dictionary")
      For i = LBound(a, 1) To UBound(a, 1)
        If a(i, 1) = Me.ComboBox1 And a(i, 2) = Me.ComboBox2 And a(i, 3) = Me.ComboBox3 Then mondico(a(i, 4)) = ""
      Next i
      Me.ComboBox4.List = mondico.keys
      'Me.TextBox1 = Me.ComboBox3
    End Sub
     
    ''--------------------------------------
     
    Private Sub ComboBox4_click()
     
      Me.TextBox1 = Me.ComboBox4
      'Me.Label22 = Me.ComboBox4     'resultat de la combobox 4 decale d'une cellule a droite
     
    End Sub

  2. #2
    Membre Expert Avatar de Thautheme
    Homme Profil pro
    salarié
    Inscrit en
    Août 2014
    Messages
    1 373
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : salarié

    Informations forums :
    Inscription : Août 2014
    Messages : 1 373
    Par défaut
    Bonjour Ciambe, bonjour le forum,

    Si ta ComboBox4 ne contient qu'un seul élément essaie comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Private Sub ComboBox4_click()
    Dim R As Range
     
    Me.TextBox1 = Me.ComboBox4
    Set R = f.Columns(4).Find(Me.ComboBox4.Value, , xlValues, xlWhole)
    If Not R Is Nothing Then Me.Label22 = R.Offset(0, 1).Value
    End Sub
    Sinon, on peut stocker le numéro de ligne dans la ComboBox4 dans une colonne masquée :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    Private Sub UserForm_Click()
    Dim f, a()
     
    Private Sub UserForm_Initialize()
    Set f = Sheets("Cv_Tapis")
    Set mondico = CreateObject("Scripting.Dictionary")
    a = f.Range("A3:E" & f.[A65000].End(xlUp).Row).Value
    For I = LBound(a, 1) To UBound(a, 1)
    mondico(a(I, 1)) = ""
    Next I
    Me.ComboBox4.ColumnCount = 2
    Me.ComboBox4.ColumnWidths = "0"
    Me.ComboBox1.List = mondico.keys
    End Sub
     
    Private Sub ComboBox3_click()
    Me.ComboBox4.Clear
    For I = LBound(a, 1) To UBound(a, 1)
        If a(I, 1) = Me.ComboBox1 And a(I, 2) = Me.ComboBox2 And a(I, 3) = Me.ComboBox3 Then
            With Me.ComboBox4
                .AddItem
                .Column(0, .ListCount - 1) = I
                .Column(1, .ListCount - 1) = a(I, 4)
            End With
        End If
    Next I
    End Sub
    Et ensuite on le récupère :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Private Sub ComboBox4_click()
    Dim LI As Integer
    Me.TextBox1 = Me.ComboBox4
    LI = Me.ComboBox4.Column(0, Me.ComboBox4.ListIndex)
    If LI <> 0 Then Me.Label22 = f.Cells(LI, 5).Value
    End Sub


  3. #3
    Membre averti
    Homme Profil pro
    dessinateur
    Inscrit en
    Février 2013
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : dessinateur
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2013
    Messages : 27
    Par défaut
    Bonjour Thautheme,
    Merci pour la réponse,
    La 1 er solution fonctionne très bien et devrait me suffire pour mon appli
    La deuxième j’ai problème de lecture lorsque le choix ComboBox4 est fait
    Le Label m’affiche le résultat de ligne au-dessus (Exemple : Combobox 4 ligne 11 -> Label ligne 9)
    Mon tableau commence en ligne 3 les é première sont des en-têtes c’est pour cela.
    Une autre question j’aimerai lorsque je click sur les combobox le label et le textbox s’efface comme pour les combobox. Me.Label.Clear ne fonctionne pas ?

  4. #4
    Membre Expert Avatar de Thautheme
    Homme Profil pro
    salarié
    Inscrit en
    Août 2014
    Messages
    1 373
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : salarié

    Informations forums :
    Inscription : Août 2014
    Messages : 1 373
    Par défaut
    Re,

    Si il y a décalage il suffit de rajouter +2 à la variable LI. Sinon pour vider une label : Label1.Caption = "" et une textbox : TextBox1.Value = ""...

  5. #5
    Membre averti
    Homme Profil pro
    dessinateur
    Inscrit en
    Février 2013
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : dessinateur
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2013
    Messages : 27
    Par défaut
    Merci pour les réponses,
    Avant votre réponse, j'ai mis +2 après ListIndex qui donne je crois le début de la ComboBox mais ça ne vas pas.
    Ou doit je le rajouter.
    Merci pour votre aide.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Private Sub ComboBox4_click()
    Dim LI As Integer
    Me.TextBox1 = Me.ComboBox4
    LI = Me.ComboBox4.Column(0, Me.ComboBox4.ListIndex+2)
    If LI <> 0 Then Me.Label22 = f.Cells(LI, 5).Value
    End Sub

  6. #6
    Membre Expert Avatar de Thautheme
    Homme Profil pro
    salarié
    Inscrit en
    Août 2014
    Messages
    1 373
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : salarié

    Informations forums :
    Inscription : Août 2014
    Messages : 1 373
    Par défaut
    Re,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Private Sub ComboBox4_click()
    Dim LI As Integer
    Me.TextBox1 = Me.ComboBox4
    LI = Me.ComboBox4.Column(0, Me.ComboBox4.ListIndex)
    If LI <> 0 Then Me.Label22 = f.Cells(LI + 2, 5).Value
    End Sub

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Récupérer la valeur d'une combobox
    Par kelegan dans le forum VB.NET
    Réponses: 7
    Dernier message: 02/02/2010, 15h37
  2. Récupérer les valeurs d'une combobox
    Par Many31 dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 13/07/2009, 15h16
  3. Réponses: 1
    Dernier message: 29/07/2006, 10h08
  4. Récupérer la valeur d'une combobox
    Par Invité dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 31/03/2006, 15h20
  5. Réponses: 2
    Dernier message: 29/05/2005, 19h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo