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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
Option Explicit
Public Sub fusion_Click()
' les variables
' variables pour les feuilles de classeurs et les classeurs
Dim onglet_fusionA As Worksheet
Dim onglet_fusionb As Worksheet
Dim fichier_fusionA As Workbook
Dim fichier_fusionB As Workbook
' variables pour les nb de ligne d'une feuille
Dim nblf1 As Integer
Dim nblf2 As Integer
Dim nblf3 As Integer
Dim nblf4 As Integer
Dim nblf5 As Integer
Dim nblf6 As Integer
Dim nblf7 As Integer
Dim nblf8 As Integer
Dim nblf9 As Integer
Dim nblf10 As Integer
Dim nblent1 As Integer
Dim nblent2 As Integer
Dim nblent3 As Integer
Dim nblent4 As Integer
Dim nblent5 As Integer
Dim nblent6 As Integer
Dim nblent7 As Integer
Dim nblent8 As Integer
Dim nblent9 As Integer
Dim nblent10 As Integer
' variables pour les nb de ligne total
Dim nbltot1 As Integer
Dim nbltot2 As Integer
Dim nbltot3 As Integer
Dim nbltot4 As Integer
Dim nbltot5 As Integer
Dim nbltot6 As Integer
Dim nbltot7 As Integer
Dim nbltot8 As Integer
Dim nbltot9 As Integer
Dim nbltot10 As Integer
' variables pour les nb de colonne d'une feuille
Dim nbcf1 As Integer
Dim nbcf2 As Integer
Dim nbcf3 As Integer
Dim nbcf4 As Integer
Dim nbcf5 As Integer
Dim nbcf6 As Integer
Dim nbcf7 As Integer
Dim nbcf8 As Integer
Dim nbcf9 As Integer
Dim nbcf10 As Integer
Dim nbcent1 As Integer
Dim nbcent2 As Integer
Dim nbcent3 As Integer
Dim nbcent4 As Integer
Dim nbcent5 As Integer
Dim nbcent6 As Integer
Dim nbcent7 As Integer
Dim nbcent8 As Integer
Dim nbcent9 As Integer
Dim nbcent10 As Integer
' variables pour les nb de colonne total
Dim nbctot1 As Integer
Dim nbctot2 As Integer
Dim nbctot3 As Integer
Dim nbctot4 As Integer
Dim nbctot5 As Integer
Dim nbctot6 As Integer
Dim nbctot7 As Integer
Dim nbctot8 As Integer
Dim nbctot9 As Integer
Dim nbctot10 As Integer
' declaration de variable compteur
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim cpt1 As Integer
Dim cpt2 As Integer
Dim nblp1 As Integer
Dim nblp2 As Integer
Dim nblp3 As Integer
Dim nblp4 As Integer
Dim nblp5 As Integer
Dim nblp6 As Integer
Dim nblp7 As Integer
Dim nblp8 As Integer
Dim nblp9 As Integer
Dim nblp10 As Integer
' variable pour l utilisation des chaines de caractères
Dim chaine1 As String
Dim chaine2 As String
Dim chaine3 As String
Dim chaine4 As String
Dim chaine5 As String
Dim chaine6 As String
Dim chaine7 As String
Dim chaine8 As String
Dim chaine9 As String
Dim chaine10 As String
' variable pour les classeurs
Dim wbk1 As Workbook
Dim wbk2 As Workbook
Dim wbk3 As Workbook
Dim wbk4 As Workbook
Dim wbk5 As Workbook
Dim wbk6 As Workbook
Dim wbk7 As Workbook
Dim wbk8 As Workbook
Dim wbk9 As Workbook
Dim wbk10 As Workbook
Dim wbkf1 As Workbook
Dim wbkf2 As Workbook
' variable pour des objets
Dim xlsApp1 As Object
Dim xlsApp2 As Object
' creation de tableaux à taille dynamique pour le deplacement des morceaux de tableau par bloc
Dim tabdyna1()
Dim tabdyna2()
Dim tabdyna3()
Dim tabdyna4()
' calcul du nombre de lignes et colonnes du fichier d'entete (onglet A) , ici on est directement dans le fichier avec les lignes d'entetes ou il y a la macro dans le même fichier .
' il est inutile d ouvrir le fichier puisque on est déjà dedans ? Pourtant le compilateur VBa dit que le fichier n est pas ouvert !!! illogique ...
ActiveWorkbook.Worksheets("ongleta").Activate
'wb.Worksheets("Gestion_Boite").Activate
nblent1 = ActiveSheet.UsedRange.Rows.Count
nbcent1 = ActiveSheet.UsedRange.Columns.Count
' rangement dans le tableau a taille variable , on ne sait pas combien de lignes seront empilées ...
tabdyna1(1, nbcent1 - 8) = Range("A" & 1)
tabdyna1(1, nbcent1 - 7) = Range("B" & 1)
tabdyna1(1, nbcent1 - 6) = Range("C" & 1)
tabdyna1(1, nbcent1 - 5) = Range("D" & 1)
tabdyna1(1, nbcent1 - 4) = Range("E" & 1)
tabdyna1(1, nbcent1 - 3) = Range("F" & 1)
tabdyna1(1, nbcent1 - 2) = Range("G" & 1)
tabdyna1(1, nbcent1 - 1) = Range("H" & 1)
tabdyna1(1, nbcent1) = Range("I" & 1)
cpt1 = nblent1
' calcul du nombre de lignes et colonnes du fichier d'entete (onglet B)
ActiveWorkbook.Worksheets("ongletb").Activate
'ActiveWorkbook.Sheets ("ongletb")
nblent2 = ActiveSheet.UsedRange.Rows.Count
nbcent2 = ActiveSheet.UsedRange.Columns.Count
' rangement dans le tableau a taille variable , on ne sait pas combien de lignes seront empilées ..
tabdyna2(1, nbcent2 - 5) = Range("A" & 1)
tabdyna2(1, nbcent2 - 4) = Range("B" & 1)
tabdyna2(1, nbcent2 - 3) = Range("C" & 1)
tabdyna2(1, nbcent2 - 2) = Range("D" & 1)
tabdyna2(1, nbcent2 - 1) = Range("E" & 1)
tabdyna2(1, nbcent2) = Range("F" & 1)
cpt2 = nblent2
' fichier de donnees
' fichier 1 -------------------------
' fichier 1 onglet A
wbk1 = Workbooks.Open(Filename:="P:\fichier_donnees1.xlsx")
wbk1.Worksheets("ongleta").Activate
nblf1 = ActiveSheet.UsedRange.Rows.Count
nbcf1 = ActiveSheet.UsedRange.Columns.Count
For i = (cpt1 + 1) To nblf1 ' toujour un remplissage à la ligne ligne+1
tabdyna1(nblf1, nbcf1 - 8) = Range("A" & nblf1)
tabdyna1(nblf1, nbcf1 - 7) = Range("B" & nblf1)
tabdyna1(nblf1, nbcf1 - 6) = Range("C" & nblf1)
tabdyna1(nblf1, nbcf1 - 5) = Range("D" & nblf1)
tabdyna1(nblf1, nbcf1 - 4) = Range("E" & nblf1)
tabdyna1(nblf1, nbcf1 - 3) = Range("F" & nblf1)
tabdyna1(nblf1, nbcf1 - 2) = Range("G" & nblf1)
tabdyna1(nblf1, nbcf1 - 1) = Range("H" & nblf1)
tabdyna1(nblf1, nbcf1) = Range("I" & nblf1)
Next
cpt1 = cpt1 + nblf1
' fichier 1 onglet B
wbk2 = Workbooks.Open(Filename:="P:\fichier_donnees1.xlsx")
wbk2.Worksheets("ongletb").Activate
nblf2 = ActiveSheet.UsedRange.Rows.Count
nbcf2 = ActiveSheet.UsedRange.Columns.Count
For i = (cpt2 + 1) To nblf2 ' toujour un remplissage à la ligne ligne+1
tabdyna1(nblf2, nbcf2 - 5) = Range("A" & nblf2)
tabdyna1(nblf2, nbcf2 - 4) = Range("B" & nblf2)
tabdyna1(nblf2, nbcf2 - 3) = Range("C" & nblf2)
tabdyna1(nblf2, nbcf2 - 2) = Range("D" & nblf2)
tabdyna1(nblf2, nbcf2 - 1) = Range("E" & nblf2)
tabdyna1(nblf2, nbcf2) = Range("F" & nblf2)
Next
cpt2 = cpt2 + nblf2
' fin traitement fichier donnees 1
'fichier 2
' fichier 2 onglet A
wbk2 = Workbooks.Open(Filename:="P:\fichier_donnees2.xlsx")
wbk2.Worksheets("ongleta").Activate
nblf3 = ActiveSheet.UsedRange.Rows.Count
nbcf1 = ActiveSheet.UsedRange.Columns.Count
For i = (cpt1 + 1) To nblf3 ' toujour un remplissage à la ligne ligne+1
tabdyna1(nblf3, nbcf1 - 8) = Range("A" & nblf3)
tabdyna1(nblf3, nbcf1 - 7) = Range("B" & nblf3)
tabdyna1(nblf3, nbcf1 - 6) = Range("C" & nblf3)
tabdyna1(nblf3, nbcf1 - 5) = Range("D" & nblf3)
tabdyna1(nblf3, nbcf1 - 4) = Range("E" & nblf3)
tabdyna1(nblf3, nbcf1 - 3) = Range("F" & nblf3)
tabdyna1(nblf3, nbcf1 - 2) = Range("G" & nblf3)
tabdyna1(nblf3, nbcf1 - 1) = Range("H" & nblf3)
tabdyna1(nblf3, nbcf1) = Range("I" & nblf3)
Next
cpt1 = cpt1 + nblf3
' fichier 2 onglet B
wbk4 = Workbooks.Open(Filename:="P:\fichier_donnees2.xlsx")
wbk4.Worksheets("ongletb").Activate
nblf4 = ActiveSheet.UsedRange.Rows.Count
nbcf2 = ActiveSheet.UsedRange.Columns.Count
For i = (cpt2 + 1) To nblf4 ' toujour un remplissage à la ligne ligne+1
tabdyna2(nblf4, nbcf2 - 5) = Range("A" & nblf4)
tabdyna2(nblf4, nbcf2 - 4) = Range("B" & nblf4)
tabdyna2(nblf4, nbcf2 - 3) = Range("C" & nblf4)
tabdyna2(nblf4, nbcf2 - 2) = Range("D" & nblf4)
tabdyna2(nblf4, nbcf2 - 1) = Range("E" & nblf4)
tabdyna2(nblf4, nbcf2) = Range("F" & nblf4)
Next
cpt2 = cpt2 + nblf4
'fin traitement fichier donnees 2
'fichier 3
' fichier 3 onglet A
wbk3 = Workbooks.Open(Filename:="P:\fichier_donnees3.xls")
wbk3.Worksheets("ongleta").Activate
nblf5 = ActiveSheet.UsedRange.Rows.Count
nbcf1 = ActiveSheet.UsedRange.Columns.Count
For i = (cpt1 + 1) To nblf5 ' toujour un remplissage à la ligne ligne+1
tabdyna1(nblf5, nbcf1 - 8) = Range("A" & nblf5)
tabdyna1(nblf5, nbcf1 - 7) = Range("B" & nblf5)
tabdyna1(nblf5, nbcf1 - 6) = Range("C" & nblf5)
tabdyna1(nblf5, nbcf1 - 5) = Range("D" & nblf5)
tabdyna1(nblf5, nbcf1 - 4) = Range("E" & nblf5)
tabdyna1(nblf5, nbcf1 - 3) = Range("F" & nblf5)
tabdyna1(nblf5, nbcf1 - 2) = Range("G" & nblf5)
tabdyna1(nblf5, nbcf1 - 1) = Range("H" & nblf5)
tabdyna1(nblf5, nbcf1) = Range("I" & nblf5)
Next
cpt1 = cpt1 + nblf5
' fichier 3 onglet B
wbk6 = Workbooks.Open(Filename:="P:\fichier_donnees3.xls")
wbk6.Worksheets("ongletb").Activate
nblf6 = ActiveSheet.UsedRange.Rows.Count
nbcf2 = ActiveSheet.UsedRange.Columns.Count
For i = (cpt2 + 1) To nblf6 ' toujour un remplissage à la ligne ligne+1
tabdyna2(nblf6, nbcf2 - 5) = Range("A" & nblf6)
tabdyna2(nblf6, nbcf2 - 4) = Range("B" & nblf6)
tabdyna2(nblf6, nbcf2 - 3) = Range("C" & nblf6)
tabdyna2(nblf6, nbcf2 - 2) = Range("D" & nblf6)
tabdyna2(nblf6, nbcf2 - 1) = Range("E" & nblf6)
tabdyna2(nblf6, nbcf2) = Range("F" & nblf6)
Next
cpt2 = cpt2 + nblf6
'fin traitement fichier donnees 3
'fin d'import des données dans les feuilles excel dans les tableaux virtuels dynamiques
' nommination des futures feuilles et classeurs excel
chaine1 = "P:\fusion_ongleta.xlsx"
chaine2 = "P:\fusion_ongletb.xlsx"
chaine3 = "ongleta"
chaine4 = "ongletb"
' creation des fichiers de sorties
'xlApp1 = CreateObject("Excel.Application", chaine1)
'xlApp2 = CreateObject("Excel.Application", chaine2)
' ouverture des fichiers excel nouvellement créés
wbkf1 = Workbooks.Open(Filename:=chaine1)
'xlSheet1 = xlApp1.Createworksheets(chaine3)
wbkf1.Worksheets("ongleta").Activate
wbkf2 = Workbooks.Open(Filename:=chaine2)
'xlSheet2 = xlApp2.Createworksheets(chaine4)
wbkf2.Worksheets("ongletb").Activate
' compacter les lignes dans les feuilles créés
' onglet de fusion A >> injection des données
For i = 1 To cpt1 ' ligne par ligne
Worksheets(chaine3).Add(xlSrcRange, Range("A" & i), xlYes) = tabdyna1(i, nbcf1 - 8)
Worksheets(chaine3).Add(xlSrcRange, Range("B" & i), xlYes) = tabdyna1(i, nbcf1 - 7)
Worksheets(chaine3).Add(xlSrcRange, Range("C" & i), xlYes) = tabdyna1(i, nbcf1 - 6)
Worksheets(chaine3).Add(xlSrcRange, Range("D" & i), xlYes) = tabdyna1(i, nbcf1 - 5)
Worksheets(chaine3).Add(xlSrcRange, Range("E" & i), xlYes) = tabdyna1(i, nbcf1 - 4)
Worksheets(chaine3).Add(xlSrcRange, Range("F" & i), xlYes) = tabdyna1(i, nbcf1 - 3)
Worksheets(chaine3).Add(xlSrcRange, Range("G" & i), xlYes) = tabdyna1(i, nbcf1 - 2)
Worksheets(chaine3).Add(xlSrcRange, Range("H" & i), xlYes) = tabdyna1(i, nbcf1 - 1)
Worksheets(chaine3).Add(xlSrcRange, Range("I" & i), xlYes) = tabdyna1(i, nbcf1)
Next
' onglet de fusion B >> injection des données
For i = 1 To cpt2 ' ligne par ligne
Worksheets(chaine4).Add(xlSrcRange, Range("A" & i), xlYes) = tabdyna1(i, nbcf2 - 5)
Worksheets(chaine4).Add(xlSrcRange, Range("B" & i), xlYes) = tabdyna1(i, nbcf2 - 4)
Worksheets(chaine4).Add(xlSrcRange, Range("C" & i), xlYes) = tabdyna1(i, nbcf2 - 3)
Worksheets(chaine4).Add(xlSrcRange, Range("D" & i), xlYes) = tabdyna1(i, nbcf2 - 2)
Worksheets(chaine4).Add(xlSrcRange, Range("E" & i), xlYes) = tabdyna1(i, nbcf2 - 1)
Worksheets(chaine4).Add(xlSrcRange, Range("F" & i), xlYes) = tabdyna1(i, nbcf2)
Next
End Sub |