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 :

Raccourcir un code


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    300
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 300
    Par défaut Raccourcir un code
    Re-Bonjour

    Voilà, j'ai un code qui fonctionne, mais qui est super long, pour apprendre et pour info, y' a t-il un moyen de le raccourcir ??? Merci à vous
    sur l'exemple ci-dessous il y a 4 Textbox, mais mon code en comporte 25.

    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
    Worksheets(Label2.Caption).Activate
        If TextBox2 = "" Then
        Else
        Range("C10").Select
        ActiveCell.Value = ActiveCell.Value - TextBox2.Value
        End If
            Worksheets(Label3.Caption).Activate
            If TextBox3 = "" Then
            Else
            Range("C10").Select
            ActiveCell.Value = ActiveCell.Value - TextBox3.Value
            End If
                Worksheets(Label4.Caption).Activate
                If TextBox4 = "" Then
                Else
                Range("C10").Select
                ActiveCell.Value = ActiveCell.Value - TextBox4.Value
                End If
                    Worksheets(Label5.Caption).Activate
                    If TextBox5 = "" Then
                    Else
                    Range("C10").Select
                    ActiveCell.Value = ActiveCell.Value - TextBox5.Value
                    End If

  2. #2
    Membre Expert Avatar de laetitia
    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    1 281
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France

    Informations forums :
    Inscription : Décembre 2002
    Messages : 1 281
    Par défaut
    bonjour Didpa pas clair ton code il est sense faire quoi ??? les textboxs sont dans un userform les labels aussi ???. si tu veus que quelqu'un ou 'une t'aide il faut être plus explicite ou zipper un exemple

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    300
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 300
    Par défaut
    Les labels sont des articles, et les textbox des quantités, qui sont récupérées dans une feuille ou la liste change,
    Dans mon userform, la label, prend la caption du premier article et sa quantité, un si de suite.

    Voici le code en entier.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    Private Sub CommandButton1_Click()
    ' rempli la feuille stock
    Worksheets("Stock").Activate
    If TextBox1 = "" Then
    Else
    Range("C10").Select
    ActiveCell.Value = ActiveCell.Value - TextBox1.Value
    End If
        Worksheets(Label2.Caption).Activate
        If TextBox2 = "" Then
        Else
        Range("C10").Select
        ActiveCell.Value = ActiveCell.Value - TextBox2.Value
        End If
            Worksheets(Label3.Caption).Activate
            If TextBox3 = "" Then
            Else
            Range("C10").Select
            ActiveCell.Value = ActiveCell.Value - TextBox3.Value
            End If
                Worksheets(Label4.Caption).Activate
                If TextBox4 = "" Then
                Else
                Range("C10").Select
                ActiveCell.Value = ActiveCell.Value - TextBox4.Value
                End If
                    Worksheets(Label5.Caption).Activate
                    If TextBox5 = "" Then
                    Else
                    Range("C10").Select
                    ActiveCell.Value = ActiveCell.Value - TextBox5.Value
                    End If
    Worksheets(Label6.Caption).Activate
    If TextBox6 = "" Then
    Else
    Range("C10").Select
    ActiveCell.Value = ActiveCell.Value - TextBox6.Value
    End If
        Worksheets(Label7.Caption).Activate
        If TextBox7 = "" Then
        Else
        Range("C10").Select
        ActiveCell.Value = ActiveCell.Value - TextBox7.Value
        End If
            Worksheets(Label8.Caption).Activate
            If TextBox8 = "" Then
            Else
            Range("C10").Select
            ActiveCell.Value = ActiveCell.Value - TextBox8.Value
            End If
                Worksheets(Label9.Caption).Activate
                If TextBox9 = "" Then
                Else
                Range("C10").Select
                ActiveCell.Value = ActiveCell.Value - TextBox9.Value
                End If
                    Worksheets(Label10.Caption).Activate
                    If TextBox10 = "" Then
                    Else
                    Range("C10").Select
                    ActiveCell.Value = ActiveCell.Value - TextBox10.Value
                    End If
    Worksheets(Label11.Caption).Activate
    If TextBox11 = "" Then
    Else
    Range("C10").Select
    ActiveCell.Value = ActiveCell.Value - TextBox11.Value
    End If
        Worksheets(Label12.Caption).Activate
        If TextBox12 = "" Then
        Else
        Range("C10").Select
        ActiveCell.Value = ActiveCell.Value - TextBox12.Value
        End If
            Worksheets(Label13.Caption).Activate
            If TextBox13 = "" Then
            Else
            Range("C10").Select
            ActiveCell.Value = ActiveCell.Value - TextBox13.Value
            End If
                Worksheets(Label14.Caption).Activate
                If TextBox14 = "" Then
                Else
                Range("C10").Select
                ActiveCell.Value = ActiveCell.Value - TextBox14.Value
                End If
                    Worksheets(Label15.Caption).Activate
                    If TextBox15 = "" Then
                    Else
                    Range("C10").Select
                    ActiveCell.Value = ActiveCell.Value - TextBox15.Value
                    End If
    Worksheets(Label16.Caption).Activate
    If TextBox16 = "" Then
    Else
    Range("C10").Select
    ActiveCell.Value = ActiveCell.Value - TextBox16.Value
    End If
        Worksheets(Label17.Caption).Activate
        If TextBox17 = "" Then
        Else
        Range("C10").Select
        ActiveCell.Value = ActiveCell.Value - TextBox17.Value
        End If
            Worksheets(Label18.Caption).Activate
            If TextBox18 = "" Then
            Else
            Range("C10").Select
            ActiveCell.Value = ActiveCell.Value - TextBox18.Value
            End If
                Worksheets(Label19.Caption).Activate
                If TextBox19 = "" Then
                Else
                Range("C10").Select
                ActiveCell.Value = ActiveCell.Value - TextBox19.Value
                End If
                    Worksheets(Label20.Caption).Activate
                    If TextBox20 = "" Then
                    Else
                    Range("C10").Select
                    ActiveCell.Value = ActiveCell.Value - TextBox20.Value
                    End If
    Worksheets(Label21.Caption).Activate
    If TextBox21 = "" Then
    Else
    Range("C10").Select
    ActiveCell.Value = ActiveCell.Value - TextBox21.Value
    End If
        Worksheets(Label22.Caption).Activate
        If TextBox22 = "" Then
        Else
        Range("C10").Select
        ActiveCell.Value = ActiveCell.Value - TextBox22.Value
        End If
            Worksheets(Label23.Caption).Activate
            If TextBox23 = "" Then
            Else
            Range("C10").Select
            ActiveCell.Value = ActiveCell.Value - TextBox23.Value
            End If
                Worksheets(Label24.Caption).Activate
                If TextBox24 = "" Then
                Else
                Range("C10").Select
                ActiveCell.Value = ActiveCell.Value - TextBox24.Value
                End If
                    Worksheets(Label25.Caption).Activate
                    If TextBox25 = "" Then
                    Else
                    Range("C10").Select
                    ActiveCell.Value = ActiveCell.Value - TextBox25.Value
                    End If
     
    'Rempli le compte client
    Worksheets("Conso").Activate
    Range("A5").Select
    Nom = TB_Nom.Value
    Columns(1).Find(Nom, , , , , Previous).Select
     
    ActiveCell.Offset(0, 1).Select
    'Si les dettes sont réglé, remet les compte à zéro
    If OB_Dette.Value = False Then
    Else
    ActiveCell = TB_Dette.Value
    End If
    'Ajoute les consommations
    If TextBox1 = "" Then
    Else
    ActiveCell.Value = ActiveCell.Value + (TextBox1 * 1)
    End If
        If TextBox2 = "" Then
        Else
        ActiveCell.Value = ActiveCell.Value + (TextBox2 * 1)
        End If
            If TextBox3 = "" Then
            Else
            ActiveCell.Value = ActiveCell.Value + (TextBox3 * 1)
            End If
                If TextBox4 = "" Then
                Else
                ActiveCell.Value = ActiveCell.Value + (TextBox4 * 1)
                End If
                    If TextBox5 = "" Then
                    Else
                    ActiveCell.Value = ActiveCell.Value + (TextBox5 * 1)
                    End If
    If TextBox6 = "" Then
    Else
    ActiveCell.Value = ActiveCell.Value + (TextBox6 * 1)
    End If
        If TextBox7 = "" Then
        Else
        ActiveCell.Value = ActiveCell.Value + (TextBox7 * 1)
        End If
            If TextBox8 = "" Then
            Else
            ActiveCell.Value = ActiveCell.Value + (TextBox8 * 1)
            End If
                If TextBox9 = "" Then
                Else
                ActiveCell.Value = ActiveCell.Value + (TextBox9 * 1)
                End If
                    If TextBox10 = "" Then
                    Else
                    ActiveCell.Value = ActiveCell.Value + (TextBox10 * 1)
                    End If
    If TextBox11 = "" Then
    Else
    ActiveCell.Value = ActiveCell.Value + (TextBox11 * 1)
    End If
        If TextBox12 = "" Then
        Else
        ActiveCell.Value = ActiveCell.Value + (TextBox12 * 1)
        End If
            If TextBox13 = "" Then
            Else
            ActiveCell.Value = ActiveCell.Value + (TextBox13 * 1)
            End If
                If TextBox14 = "" Then
                Else
                ActiveCell.Value = ActiveCell.Value + (TextBox14 * 1)
                End If
                    If TextBox15 = "" Then
                    Else
                    ActiveCell.Value = ActiveCell.Value + (TextBox15 * 1)
                    End If
    If TextBox16 = "" Then
    Else
    ActiveCell.Value = ActiveCell.Value + (TextBox16 * 1)
    End If
        If TextBox17 = "" Then
        Else
        ActiveCell.Value = ActiveCell.Value + (TextBox17 * 1)
        End If
            If TextBox18 = "" Then
            Else
            ActiveCell.Value = ActiveCell.Value + (TextBox18 * 1)
            End If
                If TextBox19 = "" Then
                Else
                ActiveCell.Value = ActiveCell.Value + (TextBox19 * 1)
                End If
                    If TextBox20 = "" Then
                    Else
                    ActiveCell.Value = ActiveCell.Value + (TextBox20 * 1)
                    End If
    If TextBox21 = "" Then
    Else
    ActiveCell.Value = ActiveCell.Value + (TextBox21 * 1)
    End If
        If TextBox22 = "" Then
        Else
        ActiveCell.Value = ActiveCell.Value + (TextBox22 * 1)
        End If
            If TextBox23 = "" Then
            Else
            ActiveCell.Value = ActiveCell.Value + (TextBox23 * 1)
            End If
                If TextBox24 = "" Then
                Else
                ActiveCell.Value = ActiveCell.Value + (TextBox24 * 1)
                End If
                    If TextBox25 = "" Then
                    Else
                    ActiveCell.Value = ActiveCell.Value + (TextBox25 * 1)
                    End If
    'reinitialise l'userform
    Unload CSLG
    CSLG.Show
     
    End Sub
    Cordialement

  4. #4
    Membre Expert Avatar de laetitia
    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    1 281
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France

    Informations forums :
    Inscription : Décembre 2002
    Messages : 1 281
    Par défaut
    re, comme je pense comprendre tu as une feuille par article qui porte le nom d'un label non oui ???

    si c'est cela pas complique a faire cela devrait se resumer en quelques lignes de code. Pas le temps de rester... regarderais en fin d'apres midi si pas de reponse d'ici la a+

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    300
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 300
    Par défaut
    oui, c'est cela

  6. #6
    Membre Expert
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    2 130
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 2 130
    Par défaut
    Salut Didpa et le forum
    oui, c'est cela
    Court, concis... et qui ne sert à rien !!!
    Quel est ton but ? ne pas trop te fatiguer... ou qu'on puisse t'aider ? Arracher les infos ne fait pas partie de ce qui m'intéresse !

    Ton code explique tes lacunes :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    If OB_Dette.Value = False Then
    Else
    ActiveCell = TB_Dette.Value
    End If
    Si je lis un tel code :
    si OB_Dette est faux, ne rien faire, sinon, mettre la valeur de TB_Dette dans la cellule active.
    Ce qui donne en français : Si OB_Dette est vraie, alors mettre TB_Dette dans la cellule active donc, on pourrait avoir
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    If OB_Dette Then ActiveCell = TB_Dette
    Plus simple, mais pas forcément fonctionnant correctement, puisque qu'on n'a pas le but à atteindre.

    Je n'ai pas traité toute ta macro, juste le début, avec ce que j'en ai compris
    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
    Private Sub CommandButton1_Click()
    Dim X As Integer
    ' rempli la feuille stock
    'ta formule en plus simple
    If Worksheets("Stock").TextBox1 <> "" Then Range("C10") = Range("C10") - Worksheets("Stock").TextBox1
    For X = 2 To 25
        With Worksheets(Me.Controls("Label" & X).Caption)
            If Me.Controls("TextBox" & X) <> "" Then Range("C10") = Range("C10") - Me.Controls("TextBox" & X)
        End With
    Next X
     
    'Rempli le compte client
    Worksheets("Conso").Activate
    Range("A5").Select
    Nom = TB_Nom.Value
    Columns(1).Find(Nom, , , , , Previous).Select
    '.......
    Juste une remarque : utiliser les Activate/Select/ActiveCell n'est pas utile et rarement nécessaire.
    Comme je ne connai rien du fichier, hormis le code, c'est difficile de vraiment faire un travail correct qui fonctionne.
    Je suis parti du postulat que tes contrôles était sur un USF. Plusieurs choses me chagrinent :
    Range("C10") - Worksheets("Stock").TextBox1
    Une TextBox, par définition contient du texte. Moi, par principe, je préfère convertir le texte en nombre, ce qui donnerait un truc du style :
    Range("C10") - cdbl(Worksheets("Stock").TextBox1)

    Une grande interrogation :
    ActiveCell.Value = ActiveCell.Value + (TextBox23 * 1)
    Pourquoi multiplier par 1 ? Ta TextBox contient une valeur booléenne que tu veux convertir en valeur numérique ? Je comprends pas. Et quand je comprends pas, je ne peux pas réellement adapter.
    A+

Discussions similaires

  1. Boucle pour raccourcir le code?
    Par jimmo dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 03/11/2014, 08h25
  2. Raccourcir un code
    Par Invité dans le forum VB.NET
    Réponses: 7
    Dernier message: 29/03/2013, 13h24
  3. [XL-2007] je voudrais raccourcir le code
    Par patricktoulon dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 26/11/2012, 20h04
  4. Raccourcir mon code
    Par UDSP50 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 11/01/2012, 13h32
  5. Raccourcir le code CSS (propriété font)
    Par khalidlyon dans le forum Mise en page CSS
    Réponses: 4
    Dernier message: 10/09/2009, 16h02

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