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 :

optimisation du code pour des combobox


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Inscrit en
    Juillet 2007
    Messages
    502
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 502
    Par défaut optimisation du code pour des combobox
    bonjour
    voila le code ci-dessous qui me sert , entre autre, à initialise des combobox.
    COmme le nombre de ligne de la feuil2 augmente au fur et à mesure de la saisie, il y at'il un moyen optimiser ce code ?
    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
    102
    103
    104
    105
    106
    107
     
    Private Sub CommandButton4_Click() 'statistique
     
    Unload UserForm1
    Load UserForm1 'charge le usf1
     
     
    UserForm1.Frame4.Caption = "                 SELECTIONNER LES CRITERES POUR LE CALCUL DU COUT ET DU NOMBRE D'INTERVENTION"
     
    UserForm1.Frame6.Visible = True 'montre la frame2
    UserForm1.Frame4.Visible = True 'montre la frame2
    UserForm1.CommandButton3.Visible = False 'enlever bt3
     
     
    UserForm1.ComboBox26.Visible = False
    UserForm1.ComboBox27.Visible = False
    UserForm1.ComboBox28.Visible = False
    UserForm1.ComboBox19.Visible = False
    UserForm1.ComboBox9.Visible = False
    UserForm1.ComboBox8.Visible = False
    UserForm1.ComboBox2.Visible = False
    UserForm1.ComboBox14.Visible = False
    UserForm1.ComboBox15.Visible = False
    UserForm1.ComboBox16.Visible = False
    UserForm1.ComboBox17.Visible = False
    UserForm1.TextBox1.Visible = False
    UserForm1.ComboBox4.Visible = False
     
    UserForm1.Label33.Visible = False
    UserForm1.Label26.Visible = False
    UserForm1.Label6.Visible = False
    UserForm1.Label17.Visible = False
    UserForm1.Label16.Visible = False
    UserForm1.Label6.Visible = False
    UserForm1.Label5.Visible = False
    UserForm1.Label12.Visible = False
    UserForm1.Label14.Visible = False
    UserForm1.Label15.Visible = False
    UserForm1.Label22.Visible = False
    UserForm1.Label37.Visible = False
    UserForm1.Label36.Visible = False
    UserForm1.Label7.Visible = False
     
     
     
     
     
    'change la couleur des combobox servant à la construction du filtre
     
    UserForm1.ComboBox24.BackColor = &H80FF80
    UserForm1.ComboBox6.BackColor = &H80FF80
    UserForm1.ComboBox5.BackColor = &H80FF80
    UserForm1.ComboBox3.BackColor = &H80FF80
    UserForm1.ComboBox22.BackColor = &H80FF80
    UserForm1.ComboBox23.BackColor = &H80FF80
    UserForm1.ComboBox4.BackColor = &H80FF80
     
    'initialisation
    UserForm1.TextBox1.Value = ""
    UserForm1.ComboBox4.Value = ""
    UserForm1.ComboBox19.Value = ""
    UserForm1.TextBox7.Value = 0 & "€"
    UserForm1.TextBox8.Value = 0
     
     
     
     
     
       Dim j24 As Integer
                 UserForm1.ComboBox24.AddItem
                 UserForm1.ComboBox6.AddItem
                 UserForm1.ComboBox5.AddItem
                 UserForm1.ComboBox3.AddItem
                 UserForm1.ComboBox23.AddItem
                 UserForm1.ComboBox22.AddItem
     
           For j24 = 4 To Sheets("feuil2").Range("b65536").End(xlUp).Row
               UserForm1.ComboBox24 = Sheets("feuil2").Range("b" & j24)
               UserForm1.ComboBox6 = Sheets("feuil2").Range("a" & j24)
               UserForm1.ComboBox5 = Sheets("feuil2").Range("d" & j24)
                UserForm1.ComboBox3 = Sheets("feuil2").Range("e" & j24)
                UserForm1.ComboBox23 = Sheets("feuil2").Range("i" & j24)
                UserForm1.ComboBox22 = Sheets("feuil2").Range("k" & j24)
     
              If UserForm1.ComboBox24.ListIndex = -1 Then UserForm1.ComboBox24.AddItem Sheets("feuil2").Range("b" & j24)
              If UserForm1.ComboBox6.ListIndex = -1 Then UserForm1.ComboBox6.AddItem Sheets("feuil2").Range("a" & j24)
              If UserForm1.ComboBox5.ListIndex = -1 Then UserForm1.ComboBox5.AddItem Sheets("feuil2").Range("d" & j24)
              If UserForm1.ComboBox3.ListIndex = -1 Then UserForm1.ComboBox3.AddItem Sheets("feuil2").Range("e" & j24)
              If UserForm1.ComboBox23.ListIndex = -1 Then UserForm1.ComboBox23.AddItem Sheets("feuil2").Range("i" & j24)
              If UserForm1.ComboBox22.ListIndex = -1 Then UserForm1.ComboBox22.AddItem Sheets("feuil2").Range("k" & j24)
     
          Next j24
     
                UserForm1.ComboBox24.ListIndex = 0 
                UserForm1.ComboBox6.ListIndex = 0 
                UserForm1.ComboBox5.ListIndex = 0 
                UserForm1.ComboBox3.ListIndex = 0 
                UserForm1.ComboBox23.ListIndex = 0 
                UserForm1.ComboBox22.ListIndex = 0 
     
     
    UserForm1.filtrestatistiquecout
     
    UserForm1.Show 'montre le usf1
     
     
    End Sub

    merci

  2. #2
    Membre chevronné

    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    453
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 453
    Par défaut
    Oublies ta boucle avec j24...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
        Dim lDerniereLigne As Long
     
        With Sheets("feuil2")
            lDerniereLigne = .Range("b65536").End(xlUp).Row
            UserForm1.ComboBox24.List = .Range("b4:b" & lDerniereLigne).Value
            UserForm1.ComboBox6.List = .Range("a4:a" & lDerniereLigne).Value
            UserForm1.ComboBox5.List = .Range("d4:d" & lDerniereLigne).Value
            ...
        End With

  3. #3
    Membre éclairé
    Inscrit en
    Juillet 2007
    Messages
    502
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 502
    Par défaut
    bonjour
    merci de ta suggestion mais il me reste les doublons à regler .



    il faut savoir que je cherche à optimise cette procedure car j'ai 7 combobox que j'ai remplir .en eliminant les doublons.

    Du fait que le nombre de ligne de ma feuille2 augmente au fur et à mesure, lors de l'appel des procedures concernées, le temps d'attente et de plus en plus long!!!!!!!






    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    ' le combobx19  hors contrat en supprimant les doublons eventuel
    UserForm1.ComboBox19.AddItem
    Dim j19 As Integer
    For j19 = 4 To Sheets("feuil2").Range("j65536").End(xlUp).Row
    UserForm1.ComboBox19 = Sheets("feuil2").Range("j" & j19)
    If UserForm1.ComboBox19.ListIndex = -1 Then UserForm1.ComboBox19.AddItem Sheets("feuil2").Range("j" & j19)
    Next j19
    UserForm1.ComboBox19.ListIndex = 0

  4. #4
    Expert confirmé
    Avatar de fring
    Homme Profil pro
    Engineering
    Inscrit en
    Février 2008
    Messages
    3 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : Belgique

    Informations professionnelles :
    Activité : Engineering

    Informations forums :
    Inscription : Février 2008
    Messages : 3 900
    Par défaut
    Bonjour,

    Si je comprend bien le code, la boucle prend une à une les lignes de la colonne j et les insère dans le Combo.

    Pourquoi ne pas utiliser la propriété RowSource ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    With Sheets("feuil2")
    x = .Cells(Rows.Count, 10).End(xlUp).Row
    UserForm1.ComboBox19.RowSource = .Range("J4:J" & x)
    End With
    Et ainsi de suite pour les autres Combo, à moins que j'ai loupé un élément.

  5. #5
    Membre éclairé Avatar de tomy7
    Profil pro
    Étudiant
    Inscrit en
    Janvier 2008
    Messages
    540
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2008
    Messages : 540
    Par défaut
    pour tes doublons: tu as des exemples comme celui-ci

    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
    Voici un exemple où 3 combobox sont remplies
    en fonction des données en colonne A, B, C.
     
    Pour en remplir d'autres, tu invoques RempliComboUnik avec les deux bons
    paramètres: la plage source et le combo de destination.
     
     
    Sub EssaiComboFillUniq()
    RempliComboUnik Sheets("Feuil1").Range("A1:A" & _
    Sheets("Feuil1").Range("A65536").End(xlUp).Row), _
    Worksheets("BD").ComboBox1
    RempliComboUnik Sheets("Feuil1").Range("B1:B" & _
    Sheets("Feuil1").Range("B65536").End(xlUp).Row), _
    Worksheets("BD").ComboBox2
    RempliComboUnik Sheets("Feuil1").Range("C1:C" & _
    Sheets("Feuil1").Range("C65536").End(xlUp).Row), _
    Worksheets("BD").ComboBox3
    End Sub
     
    Sub RempliComboUnik(Plage As Range, QuelCombo As MSForms.ComboBox)
     
    Dim C As Range
    Dim Tbl As New Collection
    Dim i As Integer
     
    On Error Resume Next
    For Each C In Plage
    If Not IsError(C) Then
    If C<>"" Then Tbl.Add C.Value, CStr(C.Value)
    End If
    Next C
    On Error GoTo 0
     
    With QuelCombo
    .Clear
    For i = 1 To Tbl.Count
    .AddItem Tbl(i)
    Next i
    .ListIndex = 0
    End With
     
    Set Tbl = Nothing
     
    End Sub

  6. #6
    Membre éclairé
    Inscrit en
    Juillet 2007
    Messages
    502
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 502
    Par défaut
    bonjour
    Fring
    au depart , cela etait interessant mais il faut que je elimine les doublons.

    je regarde vos codes

  7. #7
    Inactif  
    Avatar de ouskel'n'or
    Profil pro
    Inscrit en
    Février 2005
    Messages
    12 464
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 12 464
    Par défaut
    Pour dire quelque chose... qui raccourcit le code... mais n'optimise rien...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Dim combos, labels
        combos = Array("26", "27", "28", "19", "9", "8", "2", "14", "15", "16", "17", "4")
        For i = 0 To UBound(combos) - 1
            Me.Controls("ComboBox" & combos(i)).Visible = False
        Next
        Me.TextBox1.Visible = False
        labels = Array("33", "26", "6", "17", "16", "6", "5", "12", "14", "15", "22", "37", "36", "7")
        For i = 0 To UBound(labels) - 1
            Me.Controls("Label" & labels(i)).Visible = False
        Next

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

Discussions similaires

  1. Réponses: 25
    Dernier message: 30/04/2013, 15h57
  2. Optimiser un code pour éviter " out of memory"
    Par risack dans le forum MATLAB
    Réponses: 16
    Dernier message: 19/03/2007, 09h36
  3. generateur de code pour des tickets de cyber?
    Par andyafrique dans le forum C++Builder
    Réponses: 2
    Dernier message: 07/10/2006, 12h04
  4. Réponses: 8
    Dernier message: 14/09/2006, 16h43
  5. Réponses: 2
    Dernier message: 27/04/2006, 16h45

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