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 :

affectation des valeurs de texbox en fonction du choix d'une combobox


Sujet :

Macros et VBA Excel

  1. #1
    Candidat au Club
    Inscrit en
    Mai 2010
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Mai 2010
    Messages : 2
    Points : 2
    Points
    2
    Par défaut affectation des valeurs de texbox en fonction du choix d'une combobox
    Bonjour ayant réalisé mon premier fichier contenant des userforms, j'aurais une petite question.
    Dans un userform j'ai une combobox ou l'utilisateur choisit un numéro.
    En fonction de son choix j'aimerai faire apparaitre dans les listbox présentent sur le même userform des valeurs qui dépendent du choix de la valeur de la combobox.
    Voici mon code . Merci d'avance pour votre temps et votre aide
    Iallex

    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
     
    Private Sub userform_Initialize()
    Sheets("sauvegarde").Select
    ComboBox1.RowSource = "A2:A" & Range("A65536").End(xlUp).Row
    Dim NUM As Integer
    ComboBox1.Value = NUM
     
    TextBox22.Value = Range("B" & NUM + 1)
    TextBox23.Value = Range("C" & NUM + 1)
    TextBox1.Value = Range("D" & NUM + 1)
    TextBox2.Value = Range("E" & NUM + 1)
    TextBox3.Value = Range("F" & NUM + 1)
    TextBox4.Value = Range("G" & NUM + 1)
    TextBox5.Value = Range("H" & NUM + 1)
    TextBox6.Value = Range("I & num + 1")
    TextBox7.Value = Range("J" & NUM + 1)
    TextBox8.Value = Range("K" & NUM + 1)
    TextBox9.Value = Range("L" & NUM + 1)
    TextBox10.Value = Range("M" & NUM + 1)
    TextBox11.Value = Range("N" & NUM + 1)
    TextBox12.Value = Range("O" & NUM + 1)
    TextBox13.Value = Range("P" & NUM + 1)
    TextBox14.Value = Range("R" & NUM + 1)
    TextBox15.Value = Range("S" & NUM + 1)
    TextBox16.Value = Range("U" & NUM + 1)
    TextBox17.Value = Range("V" & NUM + 1)
    TextBox18.Value = Range("X" & NUM + 1)
    TextBox19.Value = Range("Y" & NUM + 1)
    TextBox20.Value = Range("Z" & NUM + 1)
    TextBox24.Value = Range("AP" & NUM + 1)
    TextBox25.Value = Range("AQ" & NUM + 1)
     
    End Sub
     
    Private Sub btnQuitter_Click()
    Unload Me
    Sheets("analyse").Select
    ActiveSheet.Protect "1234"
    End Sub
     
    Private Sub btnValider_Click()
     
    Dim Ligne As Long
     
    With ThisWorkbook.Sheets(2)
     
    If Modification Then
    Ligne = ActiveCell.Row
    Else
     
     
    Ligne = .Range("A65536").End(xlUp).Row + 1
    End If
     
     
     
    Sheets("sauvegarde").Unprotect "1234"
    .Cells(Ligne, 1) = Label62
    .Cells(Ligne, 2) = TextBox22
    .Cells(Ligne, 3) = TextBox23
    .Cells(Ligne, 4) = TextBox1
    .Cells(Ligne, 5) = TextBox2
    .Cells(Ligne, 6) = TextBox3
    .Cells(Ligne, 7) = TextBox4
    .Cells(Ligne, 8) = TextBox5
    .Cells(Ligne, 9) = TextBox6
    .Cells(Ligne, 10) = TextBox7
    .Cells(Ligne, 11) = TextBox8
    .Cells(Ligne, 12) = TextBox9
    .Cells(Ligne, 13) = TextBox10
    .Cells(Ligne, 14) = TextBox11
    .Cells(Ligne, 15) = TextBox12
    .Cells(Ligne, 16) = TextBox13
    .Cells(Ligne, 18) = TextBox14
    .Cells(Ligne, 19) = TextBox15
    .Cells(Ligne, 21) = TextBox16
    .Cells(Ligne, 22) = TextBox17
    .Cells(Ligne, 24) = TextBox18
    .Cells(Ligne, 25) = TextBox19
    .Cells(Ligne, 26) = TextBox20
    .Cells(Ligne, 42) = TextBox24
    .Cells(Ligne, 43) = TextBox25
    ActiveSheet.Unprotect "1234"
    Sheets("sauvegarde").Select
    Range("a65536").End(xlUp).Select
    Selection.Copy
    Sheets("ANALYSE").Select
    Range("C15").Select
    ActiveSheet.Paste
    Range("C15").Font.Bold = True
    Range("C15").HorizontalAlignment = xlCenter
    Range("C15").VerticalAlignment = xlCenter
    ActiveSheet.Protect "1234", True, True, True
    Sheets("sauvegarde").Select
    ActiveSheet.Protect "1234", True, True, True
    Sheets("ANALYSE").Select
    Range("C15").Select
    End With
    Unload Me
     
    End Sub

  2. #2
    Expert éminent sénior Avatar de mercatog
    Homme Profil pro
    Inscrit en
    Juillet 2008
    Messages
    9 435
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations forums :
    Inscription : Juillet 2008
    Messages : 9 435
    Points : 31 877
    Points
    31 877
    Par défaut
    Dans ce case de figure, il est judicieux de renommer les textbox de telle sorte qu'ils correspondent aux colonnes. ci joint proposition (avant tout nommer les textbox TBox2: correspond au textbox lié à la colonne 2: càd B; TBox11: correspond au textbox lié à la colonne 11: càd K...)
    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
    54
    55
    56
    57
    58
    59
    Option Explicit
    '------------------------------------------------------------------
    ' /!\  Les textbox doivent être renommés par exemple TBoxN où N correspend à la colonne de donnée correspondante
    '------------------------------------------------------------------
    Dim Lig As Long               'variable globale ligne où écrire
    Dim Lastlig As Long           'variable globale dernière ligne
    Dim tabCol                    'tableau reprenant les colonnes de données
     
    Public Sub ComboIni()
     
    With Sheets("sauvegarde")
       Lastlig = .Cells(Rows.Count, 1).End(xlUp).Row
       Me.ComboBox1.RowSource = .Name & "!A2:A" & Lastlig
    End With
    End Sub
     
    Private Sub UserForm_Initialize()
     
    tabCol = Array(2, 3, 5, 6, 9, 11, 13)     'ici on mets les n° de colonne, à adapter ici colonne B,C,E,F,I,K,M
    ComboIni
    End Sub
     
    Private Sub ComboBox1_Change()
    Dim j As Byte
    Dim Num As Long
     
    With Sheets("sauvegarde")
       If Me.ComboBox1.ListIndex > -1 Then
          Num = Me.ComboBox1.List(Me.ComboBox1.ListIndex)                                  'On peut surpasser ces ligne au cas où tous les n° existent et se suivent
          Lig = .Range("A2:A" & Lastlig).Find(Num, LookIn:=xlValues, lookat:=xlWhole).Row
          For j = 0 To UBound(tabCol)
             Me.Controls("TBox" & tabCol(j)).Value = .Cells(Lig, tabCol(j)).Value
          Next j
       End If
    End With
    End Sub
     
    Private Sub CommandButton1_Click()
    Dim j As Byte
     
    'Dans Lig on reprend la ligne correspondante au choix de la combobox
    'Si aucun choix n'a été fait dans la combo, cela signifie, ajout d'une nouvelle lig: Lig=LastLig+1
     
    With Sheets("sauvegarde")
       .Unprotect "1234"
       If Me.ComboBox1.ListIndex = -1 Then
          Lig = Lastlig + 1
          .Cells(Lig, 1) = .Cells(Lig - 1, 1) + 1
          ComboIni
       End If
       For j = 0 To UBound(tabCol)
          .Cells(Lig, tabCol(j)).Value = Me.Controls("TBox" & tabCol(j)).Value
          Me.Controls("TBox" & tabCol(j)).Value = ""
       Next j
       Sheets("ANALYSE").Range("C15") = .Range("A" & Lastlig)
       .Protect "1234"
    End With
    Me.ComboBox1.ListIndex = -1
    End Sub

  3. #3
    Candidat au Club
    Inscrit en
    Mai 2010
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Mai 2010
    Messages : 2
    Points : 2
    Points
    2
    Par défaut
    Pouarf Niquel, la grande classe depuis que je galère, impréssionant .
    Un grand MERCI de l'aide. je n'ai plus qu'a essayer de comprendre les lignes obscures de ce code :p.
    Cordialement.
    Alex

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

Discussions similaires

  1. [XL-2003] Remplir des Textbox fonction du choix d'une Combobox dans un USF - Newbie
    Par Vrancky dans le forum Macros et VBA Excel
    Réponses: 0
    Dernier message: 06/06/2014, 08h30
  2. [XL-2007] ListBox en fonction du choix d'une combobox
    Par maxval18 dans le forum Macros et VBA Excel
    Réponses: 21
    Dernier message: 20/04/2012, 13h36
  3. Réponses: 2
    Dernier message: 26/10/2006, 17h52
  4. Réponses: 2
    Dernier message: 17/05/2006, 21h35
  5. Réponses: 2
    Dernier message: 17/05/2006, 11h43

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