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
| Option Explicit
Sub test3()
Dim Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet, Wb1 As Workbook, Wb2 As Workbook, Wb3 As Workbook, Ch1$, Ch2$, Ch3$, i&, j%, b%
'chemin vers le doossier client à adapter
Ch1 = "C:\Users\DerJul\Desktop\Stage L3\internet\clients\mission comptable\"
'nom complet classeur matrice à adapter
Ch2 = "C:\Users\DerJul\Desktop\Stage L3\internet\Matrice prévisions clients internet.xls"
'déclaration classeur et feuille données client
Ch3 = "C:\Users\DerJul\Desktop\Stage L3\Nouvel essai\Temps prévisionnel.xls"
Set Wb1 = ThisWorkbook
Set Sh1 = Wb1.Worksheets("Feuil1")
'pour chaque ligne de la feuille données client
For i = 2 To Sh1.Cells(Rows.Count, 1).End(xlUp).Row
'si le classeur portant le nom du client n'existe pas à l'endroit spécifié
If Dir(Ch1 & Sh1.Cells(i, 1) & ".xlsm") <> "" = False Then
Workbooks.Open Ch2 'ouvrir le classeur matrice
Set Wb2 = Workbooks("Matrice prévisions clients internet.xls")
Set Sh2 = Wb2.Worksheets("prévisions en cours")
'remplir les informations dans le classeur matrice
Sh2.Cells(2, 2) = Sh1.Cells(i, 1)
Sh2.Cells(3, 2) = Sh1.Cells(i, 3)
Sh2.Cells(5, 2) = Sh1.Cells(i, 24)
Sh2.Cells(6, 3) = Sh1.Cells(i, 7)
'sauvegarder le classeur matrice sous le nom du client
Wb2.SaveAs Ch1 & Sh1.Cells(i, 1) & ".xlsm", FileFormat:=52
'pour xls ActiveWorkbook.SaveAs Ch1 & w.Cells(i, 1) & ".xls", FileFormat:=56
'pour xlsx : ActiveWorkbook.SaveAs Ch1 & w.Cells(i, 1) & ".xlsx", FileFormat:=51
Else 'sinon
Workbooks.Open Ch1 & Sh1.Cells(i, 1) & ".xlsm" 'ouvrir le classeur client
Set Wb2 = Workbooks(Sh1.Cells(i, 1) & ".xlsm")
Set Sh2 = Wb2.Worksheets("prévisions en cours")
'vérifier les infos et actualiser si nécessaire
If Sh2.Cells(2, 2) <> Sh1.Cells(i, 1) Then
Sh2.Cells(2, 2) = Sh1.Cells(i, 1)
b = True
End If
If Sh2.Cells(3, 2) <> Sh1.Cells(i, 3) Then
Sh2.Cells(3, 2) = Sh1.Cells(i, 3)
b = True
End If
If Sh2.Cells(5, 2) <> Sh1.Cells(i, 24) Then
Sh2.Cells(5, 2) = Sh1.Cells(i, 24)
b = True
End If
If Sh2.Cells(6, 3) <> Sh1.Cells(i, 7) Then
Sh2.Cells(6, 3) = Sh1.Cells(i, 7)
b = True
End If
'sauvegarder si au moins une modif
If b Then Wb2.Save
End If
'fermer le classeur créé ou modifié
Wb2.Close
Next i
For i = 2 To Sh1.Cells(Rows.Count, 1).End(xlUp).Row
Workbooks.Open Ch3
Set Wb3 = Workbooks("Temps prévisionnel.xls")
Set Sh3 = Wb3.Worksheets("Feuil1")
Next i
i = j
For j = 3 To Sh3.Cells(Rows.Count, 1).End(xlUp).Row
Sh3.Cells(j, 1) = Sh1.Cells(i, 1)
Sh3.Cells(j, 2) = Sh1.Cells(i, 2)
Sh3.Cells(j, 3) = Sh1.Cells(i, 3)
Sh3.Cells(j, 5) = Sh1.Cells(i, 5)
Sh3.Cells(j, 6) = Sh1.Cells(i, 7)
Sh3.Cells(j, 8) = Sh1.Cells(i, 4)
Wb3.Save
Wb3.Close
Next j
End Sub |
Partager