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 :

Blocage pour faire une somme dans colonne variable


Sujet :

Macros et VBA Excel

  1. #1
    Membre régulier
    Homme Profil pro
    Technicien en Automatisme
    Inscrit en
    Novembre 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Technicien en Automatisme

    Informations forums :
    Inscription : Novembre 2015
    Messages : 7
    Par défaut Blocage pour faire une somme dans colonne variable
    Bonjour,

    Je suis perdu dans mon code Je dois faire une somme dans mes colonnes que j'ajoute depuis mon Useform.

    Donc j'ai x lignes à chaque fois que j'insère ce que je veux (voir le dessin )

    Nom : capture developpez 1.PNG
Affichages : 253
Taille : 17,9 Ko


    Donc se que j'aimerais c'est que a chaque fois que j'ai Un titre comme LOT ELEC que dans ma cellule F ( dans l'image c'est F12 que je face ma somme de F14 jusque F19.)

    Mais ceci peux varier en fonction des ligne que j’incrémente a chaque fois je peux avoir 2 lignes ou 30 lignes...

    Donc je cherche a savoir quelle code je dois mettre pour qu'il me face la somme automatiquement quand j’insère un titre .
    Que quand j'insère un titre comme LOT Elec il me face la somme jusqu’à il y est une autre ligne qui est un titre et sa continue pour le nouveau titre que je met etc....

    Voici mon 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
    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
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    Dim f
     
    Private Sub b_exit_usf6_Click()
       Unload UserForm1 'masque aussi userform1
    End Sub
     
     
     
    Private Sub UserForm_Initialize()
      Set f = Sheets("BD")
      Set mondico = CreateObject("Scripting.Dictionary")    'Création du dictionaire
      For Each C In Range(f.[A2], f.[A65000].End(xlUp))
        mondico(C.Value) = C.Value
      Next C
      Me.ListBox1.List = mondico.items
      Me.ListBox1.MultiSelect = fmMultiSelectMulti
    End Sub
    Private Sub ListBox1_Change()
        Me.ListBox3.Clear
        Set mondico = CreateObject("Scripting.Dictionary")  'Après avoir sélectionner dans la Liste des Rubriques
        For Each C In Range(f.[A2], f.[D65000].End(xlUp))   'On génère un second dictionaire pour la liste des phases
            For k = 0 To Me.ListBox1.ListCount - 1
              If Me.ListBox1.Selected(k) = True Then
                If C = Me.ListBox1.List(k, 0) Then
                  temp = C.Offset(, 2)
                  mondico(temp) = temp
                End If
              End If
            Next k
        Next C
        Me.ListBox2.List = mondico.items
    End Sub
    Private Sub ListBox2_Change()
      Me.ListBox3.Clear
      For Each C In Range(f.[C2], f.[C65000].End(xlUp)) 'Après avoir sélectionner dans la list des phases
        For k = 0 To Me.ListBox2.ListCount - 1           'On affiche la liste des taches
          If Me.ListBox2.Selected(k) = True Then
            If C = Me.ListBox2.List(k, 0) Then Me.ListBox3.AddItem C.Offset(, 1)
          End If
         Next k
      Next C
    End Sub
    Private Sub CommandButton1_Click()
    Dim I As Integer, y As Integer
     
    'Dim Cel As Range
    'Set Cel = Sh.[B:B].Find("*", , , , xlByRows, xlPrevious)
    'If Cel Is Nothing Then Exit Sub
    'y = Cel.Row
     
        Set sh = Sheets("Feuil1")
        y = sh.[B:B].Find("*", , , , xlByRows, xlPrevious).Row
     
     
        With Me.ListBox1
            For I = 0 To .ListCount - 1
            If .Selected(I) = True Then
                y = y + 1
        sh.Range("B" & y).Value = .List(I)
     '   With sh.Range("A" & y & ":G" & y).Interior
     '       .Pattern = xlSolid
     '      .ThemeColor = xlThemeColorAccent6
     '       .TintAndShade = 0.799981688894314
    '          .Color = 10066431
     
       '    End With
                With sh.Range("B" & y).Font
                .Name = "Calibri"
                .Size = 9
                .Bold = True
                .ThemeColor = xlThemeColorLight2
                .TintAndShade = 0
    End With
    End If
    Next I
     
     
     
     
     
        With Me.ListBox2
            For I = 0 To .ListCount - 1
            If .Selected(I) = True Then
            y = y + 1
            sh.Range("B" & y).Value = .List(I)
            With sh.Range("B" & y).Font
                .Name = "Calibri"
                .Size = 9
                .Bold = True
                .ThemeColor = xlThemeColorLight2
                .TintAndShade = 0
            End With
            End If
            Next I
        End With
     
        With Me.ListBox3
            For I = 0 To .ListCount - 1
            If .Selected(I) = True Then
            y = y + 1
            sh.Range("B" & y).Value = .List(I)
            With sh.Range("B" & y).Font
                .Name = "Calibri"
                .FontStyle = "Normal"
                .Size = 9
                .ThemeColor = xlThemeColorAccent1
                .TintAndShade = -0.249977111117893
            End With
            End If
        Next I
        End With
     
     
        'Range("G12").Formula = "=VLOOKUP(B12,tableau3,2,0)"
        Range("G12").Formula = "=IF(ISERROR(VLOOKUP(B12,tableau3,2,False)),"""",VLOOKUP(B12,tableau3,2,False))"
     
     
        Range("C12:G12").HorizontalAlignment = xlCenter
        Range("C12:G12").VerticalAlignment = xlCenter
        Range("G12").AutoFill Range("G12:G" & Range("B65536").End(xlUp).Row)
     
        Range("F12").Formula = "=IF(ISERROR(SUM(RC[-2]*RC[-1])),"""",SUM(RC[-2]*RC[-1]))"
        Range("F12").AutoFill Range("F12:F" & Range("B65536").End(xlUp).Row)
        Range("D12").Formula = "=IF(ISERROR(VLOOKUP(B12,tableau7,4,False)),"""",VLOOKUP(B12,tableau7,4,False))"
     
        Range("D12").AutoFill Range("D12:D" & Range("B65536").End(xlUp).Row)
        Range("C12").Formula = "=IF(ISERROR(VLOOKUP(B12,tableau7,3,False)),"""",VLOOKUP(B12,tableau7,3,False))"
        Range("C12").AutoFill Range("C12:C" & Range("B65536").End(xlUp).Row)
     
     
     
     With Application
            .ScreenUpdating = False
            .DisplayStatusBar = False
            .Calculation = xlCalculationManual
    End With
     
     
     
     
     
    With Application
            .Calculation = xlCalculationAutomatic
            .DisplayStatusBar = True
            .CutCopyMode = False
            .ScreenUpdating = True
    End With
     
     
      For I = 0 To Me.ListBox1.ListCount - 1
      Me.ListBox1.Selected(I) = False
      Next I
      For I = 0 To Me.ListBox2.ListCount - 1
      Me.ListBox2.RemoveItem 0
      Next I
      For I = 0 To Me.ListBox3.ListCount - 1
      Me.ListBox3.RemoveItem 0
      Next I
      Me.ListBox1.SetFocus
    End With
     
        Range("E12:G300").Select
        With Selection.Font
            .Name = "Calibri"
            .Size = 9
    '       .Strikethrough = False
    '       .Superscript = False
    '        .Subscript = False
    '        .OutlineFont = False
    '        .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor
        End With
        Selection.Font.Bold = True
        Range("D12:D42").Select
        With Selection.Font
            .Name = "Calibri"
            .Size = 9
    '        .Strikethrough = False
    '        .Superscript = False
    '        .Subscript = False
    '        .OutlineFont = False
    '        .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor
        End With
        Range("C12:C43").Select
        With Selection.Font
        With Selection.Font
            .Name = "Calibri"
            .Size = 9
    '        .Strikethrough = False
    '        .Superscript = False
    '        .Subscript = False
    '        .OutlineFont = False
    '        .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor
        End With
        With Selection.Font
            .ThemeColor = xlThemeColorAccent2
            .TintAndShade = -0.249977111117893
        End With
        End With
        Range("A1").Select
    End Sub
    Private Sub CommandButton2_Click()
     
    Dim I As Integer, y As Integer
        Set sh = Sheets("Feuil1")
        y = sh.[B:B].Find("*", , , , xlByRows, xlPrevious).Row
     
           With Me.ListBox2
            For I = 0 To .ListCount - 1
            If .Selected(I) = True Then
            y = y
     
         End If
            Next I
        End With
     
        With Me.ListBox3
            For I = 0 To .ListCount - 1
            If .Selected(I) = True Then
            y = y + 1
            sh.Range("B" & y).Value = .List(I)
            With sh.Range("B" & y).Font
                .Name = "Calibri"
                .FontStyle = "Normal"
                .Size = 9
                .ThemeColor = xlThemeColorAccent1
                .TintAndShade = -0.249977111117893
            End With
            End If
        Next I
        End With
     
     
     
        'Range("G12").Formula = "=VLOOKUP(B12,tableau3,2,0)"
        Range("G12").Formula = "=IF(ISERROR(VLOOKUP(B12,tableau3,2,False)),"""",VLOOKUP(B12,tableau3,2,False))"
     
     
     
    Range("C12:G12").HorizontalAlignment = xlCenter
    Range("C12:G12").VerticalAlignment = xlCenter
    Range("G12").AutoFill Range("G12:G" & Range("B65536").End(xlUp).Row)
    Range("F12").Formula = "=IF(ISERROR(SUM(RC[-2]*RC[-1])),"""",SUM(RC[-2]*RC[-1]))"
    Range("F12").AutoFill Range("F12:F" & Range("B65536").End(xlUp).Row)
    Range("D12").Formula = "=IF(ISERROR(VLOOKUP(B12,tableau7,4,False)),"""",VLOOKUP(B12,tableau7,4,False))"
    Range("D12").AutoFill Range("D12:D" & Range("B65536").End(xlUp).Row)
    Range("C12").Formula = "=IF(ISERROR(VLOOKUP(B12,tableau7,3,False)),"""",VLOOKUP(B12,tableau7,3,False))"
    Range("C12").AutoFill Range("C12:C" & Range("B65536").End(xlUp).Row)
     
     
     
    End Sub
     
    Private Sub CommandButton6_Click()
    Dim I As Integer, y As Integer
        Set sh = Sheets("Feuil1")
        y = sh.[B:B].Find("*", , , , xlByRows, xlPrevious).Row
     
           With Me.ListBox2
            For I = 0 To .ListCount - 1
            If .Selected(I) = True Then
            y = y + 1
            sh.Range("B" & y).Value = .List(I)
            With sh.Range("B" & y).Font
                .Name = "Calibri"
                .Size = 9
                .Bold = True
                .ThemeColor = xlThemeColorLight2
                .TintAndShade = 0
            End With
         End If
            Next I
        End With
     
        With Me.ListBox3
            For I = 0 To .ListCount - 1
            If .Selected(I) = True Then
            y = y + 1
            sh.Range("B" & y).Value = .List(I)
            With sh.Range("B" & y).Font
                .Name = "Calibri"
                .FontStyle = "Normal"
                .Size = 9
                .ThemeColor = xlThemeColorAccent1
                .TintAndShade = -0.249977111117893
            End With
            End If
        Next I
        End With
     
     
     
        'Range("G12").Formula = "=VLOOKUP(B12,tableau3,2,0)"
        Range("G12").Formula = "=IF(ISERROR(VLOOKUP(B12,tableau3,2,False)),"""",VLOOKUP(B12,tableau3,2,False))"
     
     
     
    Range("C12:G12").HorizontalAlignment = xlCenter
    Range("C12:G12").VerticalAlignment = xlCenter
    Range("G12").AutoFill Range("G12:G" & Range("B65536").End(xlUp).Row)
    Range("F12").Formula = "=IF(ISERROR(SUM(RC[-2]*RC[-1])),"""",SUM(RC[-2]*RC[-1]))"
    Range("F12").AutoFill Range("F12:F" & Range("B65536").End(xlUp).Row)
    Range("D12").Formula = "=IF(ISERROR(VLOOKUP(B12,tableau7,4,False)),"""",VLOOKUP(B12,tableau7,4,False))"
    Range("D12").AutoFill Range("D12:D" & Range("B65536").End(xlUp).Row)
    Range("C12").Formula = "=IF(ISERROR(VLOOKUP(B12,tableau7,3,False)),"""",VLOOKUP(B12,tableau7,3,False))"
    Range("C12").AutoFill Range("C12:C" & Range("B65536").End(xlUp).Row)
     
     
    End Sub
    Je vous remercie d'avance

    Cordialement

    Damien

  2. #2
    Expert confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    3 609
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Alimentation

    Informations forums :
    Inscription : Mai 2013
    Messages : 3 609
    Par défaut
    Bonjour,

    Une façon que je vois serait de boucler tes lignes de bas en haut et inscrire le numéro de chaque ligne dont la cellule contient LOT dans un tableau.
    La première valeur de ce tableau serait la dernière ligne.

    Ce tableau te donnera toutes les limites de tes sommes

  3. #3
    Invité
    Invité(e)
    Par défaut
    Bonjour,
    Tu devrais supprimer la notion de sommes de ton tableau, en faire une bonne données brute.
    Et dans une autre feuille faire un tableau dynamique croisé l ce lui qui fera le travail se mises en forme et de calcule de sous totaux!

  4. #4
    Membre régulier
    Homme Profil pro
    Technicien en Automatisme
    Inscrit en
    Novembre 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Technicien en Automatisme

    Informations forums :
    Inscription : Novembre 2015
    Messages : 7
    Par défaut
    parmi

    Une façon que je vois serait de boucler tes lignes de bas en haut et inscrire le numéro de chaque ligne dont la cellule contient LOT dans un tableau.
    La première valeur de ce tableau serait la dernière ligne.

    Ce tableau te donnera toutes les limites de tes sommes
    J'ai essayé de faire ma boucle mais en vain...
    J'ai essayé de faire en brute avec des EQUIV ou SOMME.SI mais je n'y arrive pas..

    rdurupt,

    Tu devrais supprimer la notion de sommes de ton tableau, en faire une bonne données brute.
    Et dans une autre feuille faire un tableau dynamique croisé l ce lui qui fera le travail se mises en forme et de calcule de sous totaux!
    Je ne sais pas comment tu veux le faire le tableau dynamique... car je m'en suis jamais servit de cette fonction la


    JE suis complément perdu dans cette somme qui est pourtant basique. Mais je vais essayer de faire le tableau dynamique mais si vous avez des solutions ou des hypothèses je suis preneur

    Je vous joins le fichier pour que vous voyez

    FAPv0.4DPE-CDI_Test.xlsm

    Merci encore de votre aide

    Cordialement

    Damien

  5. #5
    Expert confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    3 609
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Alimentation

    Informations forums :
    Inscription : Mai 2013
    Messages : 3 609
    Par défaut
    L'idée de rdurupt est tout de même bonne.
    Tu pars d'une "base de données" brute et tu crées un Tableau Croisé Dynamique à partir de ces données.

    Autrement, voici 2 méthodes.
    La 1e inscrit une formule de somme, la 2e écrit simplement le résultat.

    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
    Sub Totaux()
        Dim I As Long, nbLignes As Long
        Dim Tablo
        Dim Idx As Long
     
        nbLignes = Cells(Rows.Count, "F").End(xlUp).Row
     
        ReDim Tablo(Idx)
        Tablo(Idx) = nbLignes + 1
     
        For I = nbLignes To 1 Step -1
            If Left(UCase(Range("D" & I)), 4) = "LOT " Then
                Idx = Idx + 1
                ReDim Preserve Tablo(Idx)
                Tablo(Idx) = I
            End If
        Next
     
        For I = UBound(Tablo) To 1 Step -1
            Range("F" & Tablo(I)).Formula = "=SUM(" & Range("F" & Tablo(I) + 1).Address & ":" & Range("F" & Tablo(I - 1) - 1).Address & ")"
        Next
     
    End Sub
     
    Sub Totaux2()
        Dim I As Long, nbLignes As Long
        Dim Total As Double
     
        nbLignes = Cells(Rows.Count, "F").End(xlUp).Row
     
        For I = nbLignes To 1 Step -1
            Total = Total + Range("F" & I)
            If Left(UCase(Range("D" & I)), 4) = "LOT " Then
                Range("F" & I) = Total
                Total = 0
            End If
        Next
     
    End Sub

Discussions similaires

  1. faire une somme dans en colonne
    Par Vascogil dans le forum Requêtes et SQL.
    Réponses: 1
    Dernier message: 21/06/2007, 06h13
  2. Réponses: 4
    Dernier message: 09/02/2006, 15h20
  3. Probleme pour faire une somme sous Excel
    Par Nicolas92 dans le forum Excel
    Réponses: 5
    Dernier message: 02/12/2005, 11h38
  4. Probleme pour faire une somme dans header ??
    Par snoop57 dans le forum Access
    Réponses: 7
    Dernier message: 01/12/2005, 13h40
  5. faire une somme dans un état
    Par PAINCO dans le forum Access
    Réponses: 1
    Dernier message: 23/06/2005, 19h41

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