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 :

Donnée en cache dans Excel


Sujet :

Macros et VBA Excel

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    816
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 816
    Points : 49
    Points
    49
    Par défaut Donnée en cache dans Excel
    Bonjour à tous,


    Je dispose d'un fichier Excel qui dispose de 4 macros:

    1- Se connecter à une base SQLSERVER et récupéré les données et les insert dans une feuille excel (rapport)
    2- Celle-ci execute plusieurs requête SQL dans la feuille Excel rapport et donne le resultat dans une autre feuille.
    3- creation PDF
    4-Envoi de mail.

    J'ai un problème avec la macro N*2. Comme indiqué ci dessus la macro N*2 réalise des requête SQL sur la feuille rapport (qui à été réalisé par la 1er macro)

    Le problème si je ré exécute la macro 1 (donc une 2ème fois) avec des résultats différents et que je réexecute la macro 2 on dirait qu'il prend pas les (nouvelles) données présentent dans la feuille rapport mais des données en cache.


    J'ai fait un test, j'ai complétement vidé le contenu de la feuille "rapport" et j'ai ré-execute la macro N*2. Normalement cela devrait me retourner RIEN et bien non il me ressort les anciens résultat.

    Comment faire pour "vider" se cache de données présent dans ce fichier excel.


    merci d'avance pour votre aide

    guigui69

  2. #2
    Membre averti Avatar de XLRATOR
    Homme Profil pro
    Comptable Analyste
    Inscrit en
    Août 2012
    Messages
    226
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Comptable Analyste
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2012
    Messages : 226
    Points : 356
    Points
    356
    Par défaut
    Bonsoir,
    Je crois que tu ferais mieux d'illustrer ton probléme avec des rudiments de code ou bien un attachement, parce que là sans une boule de cristal personne ne saurait t'aider.
    De tas de questions s'en suivent:
    Quelle version d'Excel tu utilises?
    Quelle téchnologie d'accés aux BD SQL tu utilises (ADO ou DAO)?
    La qualité du code en lui même, en fin de tas de choses quoi....

    Alors plus de détails n'en feraient pas mal.

    Cordialement.
    Entre la canne à pèche et le poisson c'est certainement la canne que je choisirais, puis apprendrais à m'en servir.
    Si on vous souffle une solution au complet c'est que vous n'auriez rien compris du problème.
    Consultez la , de forte chances que votre problème y figure et c'est plus vite que de poser une question et en attendre la réponse.
    FAQ Excel: http://excel.developpez.com/faq/

  3. #3
    Expert éminent

    Homme Profil pro
    Curieux
    Inscrit en
    Juillet 2012
    Messages
    5 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Curieux
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2012
    Messages : 5 073
    Points : 9 853
    Points
    9 853
    Billets dans le blog
    5
    Par défaut
    Bonjour,

    avec mon total accord avec XLRATOR quand aux infos qui nous sont nécessaires .... n'oublie pas de nous présenter la portée de tes variables.

    Car si elles sont publiques .... mon avis s'orienterait sur ce point ?

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    816
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 816
    Points : 49
    Points
    49
    Par défaut
    Oui pardon

    Voici ma 1er macro qui va se connecter à une base mssqlserver (2008r2), elle remplit différente feuille excel et surtout la feuille "Rapport-activite"
    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
     
    Sub Ecars21()
    Application.Calculation = xlManual
    Dim valce1 As String
    Dim valce2 As String
    Dim utilisateur As String
     
    valce1 = Excel.Range("RECAP!F3").Value
    valce2 = CDate(Excel.Range("RECAP!H3").Value) + 1
    valce1n = DateAdd("m", -12, valce1)
    valce2n = DateAdd("m", -12, valce2)
    'valce3 = Excel.Range("Informations!B5").Value
    'a = Split(valce3, "|")
    'id_societe = a(0)
    'id_atelier = a(1)
    'MsgBox (">>>>>" & valce1 & "<<<<>>>>" & valce2 & "<<<<>>>>" & valce1n & "<<<<" & valce2n & "<<<<")
    chemin = ActiveWorkbook.Path
     
    Set fso = CreateObject("Scripting.FileSystemObject")
    Const ForReading = 1, ForWriting = 2
    Worksheets("magasin").Range("A2:N3000").ClearContents
    Worksheets("vente-vo").Range("A2:N3000").ClearContents
    Worksheets("vente-vn").Range("A2:N3000").ClearContents
    Worksheets("rapport-activite").Range("A2:ao15000").ClearContents
    Worksheets("nb-or").Range("A2:ao3000").ClearContents
    Worksheets("apv-vente-mo").Range("A2:N3000").ClearContents
    Worksheets("RESULT").Range("a3:aj500").ClearContents
    '------------
     
    Dim cnBat2 As ADODB.Connection
    Set cnBat2 = New ADODB.Connection
    Dim strConn2 As String
    strConn2 = "PROVIDER=SQLOLEDB;"
    strConn2 = strConn2 & "DATA SOURCE=10.11.7.77;UID=usr-ecars;PWD=Ecars21;DATABASE=Cars108783"
    cnBat2.Open strConn2
    cnBat2.CommandTimeout = 380
    valce3 = Format(Excel.Range("RECAP!F3").Value, "YYYYMMDD")
    valce4 = Format(CDate(Excel.Range("RECAP!H3").Value) + 1, "YYYYMMDD")
    valce3n = Format(DateAdd("m", -12, Excel.Range("RECAP!F3").Value), "YYYYMMDD")
    valce4n = Format(DateAdd("m", -12, CDate(Excel.Range("RECAP!H3").Value) + 1), "YYYYMMDD")
     
    'MsgBox (">>>>" & valce1 & ">>>>" & valce2 & ">>>>" & valce1n & ">>>" & valce2n)
        Set f21 = fso.OpenTextFile(chemin & "\Sql\vente-pr-cars21.sql", ForReading)
                    une_variable21 = Replace(f21.ReadAll, "datedebut", valce3)
                    une_variable21 = Replace(une_variable21, "datefin", valce4)
                    une_variable21 = Replace(une_variable21, "dbn1", valce3n)
                    une_variable21 = Replace(une_variable21, "dfn1", valce4n)
    '                une_variable = Replace(une_variable, "societe", rsBat0("emp"))
    '                une_variable = Replace(une_variable, "atelier", rsBat0("taller"))
                    f21.Close
    '                Set f1 = fso.OpenTextFile(chemin & "\" & "vente-pr-cars21.txt", 2, True)
    '   f1.write (une_variable21)
    '   f1.Close
               ' MsgBox (une_variable)
        Dim rsBat21 As ADODB.Recordset
        Set rsBat21 = New ADODB.Recordset
        With rsBat21
                .ActiveConnection = cnBat2
                .Open une_variable21
                DerniereLigne = Worksheets("magasin").Range("A3000").End(xlUp).Row + 1
     
                'MsgBox (DerniereLigne)
                Worksheets("magasin").Range("A" & DerniereLigne).CopyFromRecordset rsBat21
                .Close
        End With
     
     
     
     
    '---------
     
     
    'MsgBox (">>>>" & valce1 & ">>>>" & valce2 & ">>>>" & valce1n & ">>>" & valce2n)
        Set f22 = fso.OpenTextFile(chemin & "\Sql\vente-vn-cars21.sql", ForReading)
                    une_variable22 = Replace(f22.ReadAll, "datedebut", valce1)
                    une_variable22 = Replace(une_variable22, "datefin", valce2)
                    'une_variable21 = Replace(une_variable21, "dbn1", valce1n)
                    'une_variable21 = Replace(une_variable21, "dfn1", valce2n)
    '                une_variable = Replace(une_variable, "societe", rsBat0("emp"))
    '                une_variable = Replace(une_variable, "atelier", rsBat0("taller"))
                    f22.Close
    '                Set f1 = fso.OpenTextFile(chemin & "\" & "vente-pr-cars21.txt", 2, True)
    '   f1.write (une_variable21)
    '   f1.Close
               ' MsgBox (une_variable)
        Dim rsBat22 As ADODB.Recordset
        Set rsBat22 = New ADODB.Recordset
        With rsBat22
                .ActiveConnection = cnBat2
                .Open une_variable22
                DerniereLigne = Worksheets("vente-vn").Range("A3000").End(xlUp).Row + 1
     
                'MsgBox (DerniereLigne)
                Worksheets("vente-vn").Range("A" & DerniereLigne).CopyFromRecordset rsBat22
                .Close
        End With
     
     
     
    '---------
     
     
    'MsgBox (">>>>" & valce1 & ">>>>" & valce2 & ">>>>" & valce1n & ">>>" & valce2n)
        Set f23 = fso.OpenTextFile(chemin & "\Sql\vente-vo-cars21.sql", ForReading)
                    une_variable23 = Replace(f23.ReadAll, "datedebut", valce1)
                    une_variable23 = Replace(une_variable23, "datefin", valce2)
                    'une_variable21 = Replace(une_variable21, "dbn1", valce1n)
                    'une_variable21 = Replace(une_variable21, "dfn1", valce2n)
    '                une_variable = Replace(une_variable, "societe", rsBat0("emp"))
    '                une_variable = Replace(une_variable, "atelier", rsBat0("taller"))
                    f23.Close
    '                Set f1 = fso.OpenTextFile(chemin & "\" & "vente-pr-cars21.txt", 2, True)
    '   f1.write (une_variable21)
    '   f1.Close
               ' MsgBox (une_variable)
        Dim rsBat23 As ADODB.Recordset
        Set rsBat23 = New ADODB.Recordset
        With rsBat23
                .ActiveConnection = cnBat2
                .Open une_variable23
                DerniereLigne = Worksheets("vente-vo").Range("A3000").End(xlUp).Row + 1
     
                'MsgBox (DerniereLigne)
                Worksheets("vente-vo").Range("A" & DerniereLigne).CopyFromRecordset rsBat23
                .Close
        End With
     
     
     
    '-------------------------
     
     
    '---------
     
     
    'MsgBox (">>>>" & valce1 & ">>>>" & valce2 & ">>>>" & valce1n & ">>>" & valce2n)
        Set f24 = fso.OpenTextFile(chemin & "\Sql\tps-cars21.sql", ForReading)
                    une_variable24 = Replace(f24.ReadAll, "datedebut", valce1)
                    une_variable24 = Replace(une_variable24, "datefin", valce2)
                    'une_variable21 = Replace(une_variable21, "dbn1", valce1n)
                    'une_variable21 = Replace(une_variable21, "dfn1", valce2n)
    '                une_variable = Replace(une_variable, "societe", rsBat0("emp"))
    '                une_variable = Replace(une_variable, "atelier", rsBat0("taller"))
                    f24.Close
    '                Set f1 = fso.OpenTextFile(chemin & "\" & "vente-pr-cars21.txt", 2, True)
    '   f1.write (une_variable21)
    '   f1.Close
               ' MsgBox (une_variable)
        Dim rsBat24 As ADODB.Recordset
        Set rsBat24 = New ADODB.Recordset
        With rsBat24
                .ActiveConnection = cnBat2
                .Open une_variable24
                DerniereLigne = Worksheets("Rapport-activite").Range("A3000").End(xlUp).Row + 1
     
                'MsgBox (DerniereLigne)
                Worksheets("Rapport-activite").Range("A" & DerniereLigne).CopyFromRecordset rsBat24
                .Close
        End With
     
     
     
     
    'MsgBox (">>>>" & valce1 & ">>>>" & valce2 & ">>>>" & valce1n & ">>>" & valce2n)
        Set f26 = fso.OpenTextFile(chemin & "\Sql\nb-or.sql", ForReading)
                    une_variable26 = Replace(f26.ReadAll, "datedebut", valce1)
                    une_variable26 = Replace(une_variable26, "datefin", valce2)
                    une_variable26 = Replace(une_variable26, "societe", "1")
     
                    f26.Close
    '                Set f1 = fso.OpenTextFile(chemin & "\" & "vente-pr-cars21.txt", 2, True)
    '   f1.write (une_variable21)
    '   f1.Close
               ' MsgBox (une_variable)
        Dim rsBat26 As ADODB.Recordset
        Set rsBat26 = New ADODB.Recordset
        With rsBat26
                .ActiveConnection = cnBat2
                .Open une_variable26
                DerniereLigne = Worksheets("nb-or").Range("A30").End(xlUp).Row + 1
     
                'MsgBox (DerniereLigne)
                Worksheets("nb-or").Range("A" & DerniereLigne).CopyFromRecordset rsBat26
                .Close
        End With
     
     
     
    '-------------------------
    '---------
     
    '
    'MsgBox (">>>>" & valce1 & ">>>>" & valce2 & ">>>>" & valce1n & ">>>" & valce2n)
        Set f27 = fso.OpenTextFile(chemin & "\Sql\nb-or.sql", ForReading)
                    une_variable27 = Replace(f27.ReadAll, "datedebut", valce1)
                    une_variable27 = Replace(une_variable27, "datefin", valce2)
                    une_variable27 = Replace(une_variable27, "societe", "2")
     
                    f27.Close
    '                Set f1 = fso.OpenTextFile(chemin & "\" & "vente-pr-cars21.txt", 2, True)
    '   f1.write (une_variable21)
    '   f1.Close
               ' MsgBox (une_variable)
        Dim rsBat27 As ADODB.Recordset
        Set rsBat27 = New ADODB.Recordset
        With rsBat27
                .ActiveConnection = cnBat2
                .Open une_variable27
                DerniereLigne = Worksheets("nb-or").Range("A30").End(xlUp).Row + 1
     
                'MsgBox (DerniereLigne)
                Worksheets("nb-or").Range("A" & DerniereLigne).CopyFromRecordset rsBat27
                .Close
        End With
     
     
     
    '-------------------------
    '---------
     
    '
    'MsgBox (">>>>" & valce1 & ">>>>" & valce2 & ">>>>" & valce1n & ">>>" & valce2n)
        Set f28 = fso.OpenTextFile(chemin & "\Sql\nb-or.sql", ForReading)
                    une_variable28 = Replace(f28.ReadAll, "datedebut", valce1)
                    une_variable28 = Replace(une_variable28, "datefin", valce2)
                    une_variable28 = Replace(une_variable28, "societe", "3")
     
                    f28.Close
    '                Set f1 = fso.OpenTextFile(chemin & "\" & "vente-pr-cars21.txt", 2, True)
    '   f1.write (une_variable21)
    '   f1.Close
               ' MsgBox (une_variable)
        Dim rsBat28 As ADODB.Recordset
        Set rsBat28 = New ADODB.Recordset
        With rsBat28
                .ActiveConnection = cnBat2
                .Open une_variable28
                DerniereLigne = Worksheets("nb-or").Range("A30").End(xlUp).Row + 1
     
                'MsgBox (DerniereLigne)
                Worksheets("nb-or").Range("A" & DerniereLigne).CopyFromRecordset rsBat28
                .Close
        End With
     
     
     
    '-------------------------
     
    cnBat2.Close
    Set rsBat2 = Nothing
    Set cnBat2 = Nothing
     
     
     
     
     
     
    Application.Calculate
    Application.Calculation = xlAutomatic
    End Sub

    Voici la 2eme macro qui elle s'occupe de réaliser des requete SQL sur la feuille "rapport-activité"
    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
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
     
    Sub rapportactivite()
     
     
    '----------------------------------
     
     
     
     
    'Définit le classeur servant de base de données
    Fichier = ActiveWorkbook.FullName
     
    Set Cn = New ADODB.Connection
     
    '--- Connexion ---
    With Cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=YES;"""
        .Open
    End With
     
    '... la requête ...
    texte_SQL = "SELECT Departement, Worker, SUM(spenttime) as TOTAL FROM ( [Rapport-activite$]) "
    texte_SQL = texte_SQL + " WHERE id_rubric  NOT IN ('25','61','63','72','73','76') and trav_inperiod='YES' "
    texte_SQL = texte_SQL + " GROUP BY Departement, Worker"
     
    Set Rst = Cn.Execute(texte_SQL)
     
    'Copy des données
    Sheets("RESULT").Select
    Sheets("RESULT").[E3].CopyFromRecordset Rst
     
    'Entêtes de colonne
    Dim liCount As Integer
    For liCount = 0 To Rst.Fields.Count - 1
        Sheets("RESULT").Cells(1, liCount + 1) = Rst.Fields(liCount).Name
    Next
     
     
    '--- Fermeture connexion ---
    Rst.Close
    Cn.Close
    Set Cn = Nothing
     
     
     
    '------------------------------------
     
     
    'Définit le classeur servant de base de données
    Fichier = ActiveWorkbook.FullName
     
    Set Cn = New ADODB.Connection
     
    '--- Connexion ---
    With Cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=YES;"""
        .Open
    End With
     
    '... la requête ...
    texte_SQL = "SELECT Departement,Worker, SUM(invtime)as TOTAL ,SUM(net) as TOTAL2 FROM ( [Rapport-activite$]) "
    texte_SQL = texte_SQL + " WHERE id_rubric IN ('??','01','02','04','05','06','09','27','31','32','33','34','38','41') and doc_inperiod='YES' "
    texte_SQL = texte_SQL + " GROUP BY Departement, Worker"
     
    Set Rst = Cn.Execute(texte_SQL)
     
    'Copy des données
    Sheets("RESULT").Select
    Sheets("RESULT").[A3].CopyFromRecordset Rst
     
    'Entêtes de colonne
     
    For liCount = 0 To Rst.Fields.Count - 1
        Sheets("RESULT").Cells(1, liCount + 1) = Rst.Fields(liCount).Name
    Next
     
     
    '--- Fermeture connexion ---
    Rst.Close
    Cn.Close
    Set Cn = Nothing
     
     
    '---------------------------------------
     
     
    'Définit le classeur servant de base de données
    Fichier = ActiveWorkbook.FullName
     
    Set Cn = New ADODB.Connection
     
    '--- Connexion ---
    With Cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=YES;"""
        .Open
    End With
     
    '... la requête ...
    texte_SQL = "SELECT Departement,SUM(cost) as TOTAL FROM ( [Rapport-activite$]) "
    texte_SQL = texte_SQL + " WHERE trav_inperiod='YES' "
    texte_SQL = texte_SQL + " GROUP BY Departement"
     
    Set Rst = Cn.Execute(texte_SQL)
     
    'Copy des données
    Sheets("RESULT").Select
    Sheets("RESULT").[H3].CopyFromRecordset Rst
     
    'Entêtes de colonne
     
    For liCount = 0 To Rst.Fields.Count - 1
        Sheets("RESULT").Cells(1, liCount + 1) = Rst.Fields(liCount).Name
    Next
     
     
    '--- Fermeture connexion ---
    Rst.Close
    Cn.Close
    Set Cn = Nothing
     
    '---------------------------------
     
     
     
     
     
     
     
     
     
    'Définit le classeur servant de base de données
    Fichier = ActiveWorkbook.FullName
     
    Set Cn = New ADODB.Connection
     
    '--- Connexion ---
    With Cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=YES;"""
        .Open
    End With
     
    '... la requête ...
    texte_SQL = "SELECT Service, count(tt) as total FROM (SELECT DISTINCT dossier as tt, Service FROM ( [APV-Vente-MO$]) "
    texte_SQL = texte_SQL + " WHERE type IN ('0 -Client','3 -Assurance')) as  requete1 "
    texte_SQL = texte_SQL + " GROUP BY Service "
     
    Set Rst = Cn.Execute(texte_SQL)
     
    'Copy des données
    Sheets("RESULT").Select
    Sheets("RESULT").[K3].CopyFromRecordset Rst
     
    'Entêtes de colonne
     
    For liCount = 0 To Rst.Fields.Count - 1
        Sheets("RESULT").Cells(1, liCount + 1) = Rst.Fields(liCount).Name
    Next
     
     
    '--- Fermeture connexion ---
    Rst.Close
    Cn.Close
    Set Cn = Nothing
     
     
    '---------------------------------------------
     
     
     
    '-----------------------------------------------
     
     
     
     
     
     
    'Définit le classeur servant de base de données
    Fichier = ActiveWorkbook.FullName
     
    Set Cn = New ADODB.Connection
     
    '--- Connexion ---
    With Cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=YES;"""
        .Open
    End With
     
    '... la requête ...
    texte_SQL = "SELECT Departement, Worker, SUM(invtime) as TOTAL, Type FROM ( [Rapport-activite$]) "
    texte_SQL = texte_SQL + " WHERE id_rubric IN ('??','01','02','04','05','06','09','27','31','32','33','34','38','41') and trav_inperiod='YES' "
    texte_SQL = texte_SQL + " GROUP BY Departement, Worker, Type"
     
    Set Rst = Cn.Execute(texte_SQL)
     
    'Copy des données
    Sheets("RESULT").Select
    Sheets("RESULT").[o3].CopyFromRecordset Rst
     
    'Entêtes de colonne
     
    For liCount = 0 To Rst.Fields.Count - 1
        Sheets("RESULT").Cells(1, liCount + 1) = Rst.Fields(liCount).Name
    Next
     
     
    '--- Fermeture connexion ---
    Rst.Close
    Cn.Close
    Set Cn = Nothing
     
     
    '-------------------------------
     
     
     
     
     
     
     
    'Définit le classeur servant de base de données
    Fichier = ActiveWorkbook.FullName
     
    Set Cn = New ADODB.Connection
     
    '--- Connexion ---
    With Cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=YES;"""
        .Open
    End With
     
    '... la requête ...
    texte_SQL = "SELECT Departement,Worker, SUM(spenttime)as TOTAL FROM ( [Rapport-activite$]) "
    texte_SQL = texte_SQL + " WHERE trav_inperiod='YES' AND id_rubric NOT IN ('63') "
    texte_SQL = texte_SQL + " GROUP BY Departement, Worker"
     
    Set Rst = Cn.Execute(texte_SQL)
     
    'Copy des données
    Sheets("RESULT").Select
    Sheets("RESULT").[U3].CopyFromRecordset Rst
     
    'Entêtes de colonne
     
    For liCount = 0 To Rst.Fields.Count - 1
        Sheets("RESULT").Cells(1, liCount + 1) = Rst.Fields(liCount).Name
    Next
     
     
    '--- Fermeture connexion ---
    Rst.Close
    Cn.Close
    Set Cn = Nothing
     
     
    '---------------------------------------
     
     
     
     
     
     
    'Définit le classeur servant de base de données
    Fichier = ActiveWorkbook.FullName
     
    Set Cn = New ADODB.Connection
     
    '--- Connexion ---
    With Cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=YES;"""
        .Open
    End With
     
    '... la requête ...
    texte_SQL = "SELECT Departement,Worker, SUM(spenttime)as TOTAL ,SUM(net) as TOTAL2 FROM ( [Rapport-activite$]) "
    texte_SQL = texte_SQL + " WHERE id_rubric IN ('??','01','02','04','05','06','09','27','31','32','33','34','38','41') and trav_inperiod='YES' "
    texte_SQL = texte_SQL + " GROUP BY Departement, Worker"
     
    Set Rst = Cn.Execute(texte_SQL)
     
    'Copy des données
    Sheets("RESULT").Select
    Sheets("RESULT").[AA3].CopyFromRecordset Rst
     
    'Entêtes de colonne
     
    For liCount = 0 To Rst.Fields.Count - 1
        Sheets("RESULT").Cells(1, liCount + 1) = Rst.Fields(liCount).Name
    Next
     
     
    '--- Fermeture connexion ---
    Rst.Close
    Cn.Close
    Set Cn = Nothing
     
     
    '--------------------------------------
     
     
     
     
     
     
     
    'Définit le classeur servant de base de données
    Fichier = ActiveWorkbook.FullName
     
    Set Cn = New ADODB.Connection
     
    '--- Connexion ---
    With Cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties=""Excel 12.0;HDR=YES;"""
        .Open
    End With
     
    '... la requête ...
    texte_SQL = "SELECT Departement,type, SUM(invtime)as TOTAL ,SUM(net) as TOTAL2 FROM ( [Rapport-activite$]) "
    texte_SQL = texte_SQL + " WHERE id_rubric IN ('??','01','02','04','05','06','09','27','31','32','33','34','38','41') and doc_inperiod='YES' "
    texte_SQL = texte_SQL + " GROUP BY Departement, type"
     
    Set Rst = Cn.Execute(texte_SQL)
     
    'Copy des données
    Sheets("RESULT").Select
    Sheets("RESULT").[AF3].CopyFromRecordset Rst
     
    'Entêtes de colonne
     
    For liCount = 0 To Rst.Fields.Count - 1
        Sheets("RESULT").Cells(1, liCount + 1) = Rst.Fields(liCount).Name
    Next
     
     
    '--- Fermeture connexion ---
    Rst.Close
    Cn.Close
    Set Cn = Nothing
     
    End Sub
    merci d'avance pour vos explications

    guigui69

Discussions similaires

  1. importatiin données de formulaire dans Excel
    Par swatchy dans le forum InfoPath
    Réponses: 0
    Dernier message: 22/10/2015, 20h24
  2. [XL-2003] Supprimer des lignes "cachés" dans excel
    Par antoinepoirier dans le forum Macros et VBA Excel
    Réponses: 12
    Dernier message: 07/08/2015, 17h54
  3. Réponses: 7
    Dernier message: 21/09/2014, 09h29
  4. Envoi de données de Labview dans Excel
    Par clemperson dans le forum LabVIEW
    Réponses: 2
    Dernier message: 10/02/2012, 22h11
  5. Volume de données maximum à passer dans Excel via delphi?
    Par batounet dans le forum Bases de données
    Réponses: 2
    Dernier message: 24/03/2006, 20h37

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