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
| Sub Commande12_Click()
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlBook As Excel.Workbook
Dim rec0, rec1, rec2 As Recordset
Dim I As Long, J As Long
Dim Rep As String
Dim DateMax As Date
Dim Pas As Double
'Definir le Pas
Pas = -7
Do
Rep = InputBox("Saisir Date max periode observée ?")
Loop While (Not IsDate(Rep))
DateMax = CDate(Rep)
DateMin = DateAdd("d", Pas, DateMax)
MsgBox DateMin
'Date_min = InputBox("Selectionner la date de JC de debut", "DATE DEBUT", Date)
'Date_max = InputBox("Entrez la date de début voulu" & vbCrLf & "format (JJ/MM/YYYY)")
'Date_max = InputBox("Selectionner la date de JC de fin", "DATE FIN", Date)
'Date_min = DateAdd("d", -7, Date_max)
Set rec1 = CurrentDb.OpenRecordset("select distinct Nom from Table1 where [Date JC] between #" & Format(DateMin, "mm/dd/yyyy") & "# and #" & Format(DateMax, "mm/dd/yyyy") & "#;", dbOpenSnapshot)
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
' Repert = "C:\Users\gk\Documents\TEST\" & "Copie_de_TB.xls"
' MsgBox Repert
' xlApp.Workbooks.Open Repert, 0
' MsgBox "Testons"
While Not rec1.EOF
' Création du classeur
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
Set rec2 = CurrentDb.OpenRecordset("select * from Table1 where Nom ='" _
& rec1.Fields("Nom") & "' and [Date JC] between #" & Format(DateMin, "mm/dd/yyyy") & "# and #" & Format(DateMax, "mm/dd/yyyy") & "#;", dbOpenSnapshot)
' Chargement des enregistrements
' Entête
I = 1
For J = 0 To rec2.Fields.Count - 1
xlSheet.Cells(I, J + 1) = rec2.Fields(J).Name
Next J
I = 2
While Not rec2.EOF
' Détail
For J = 0 To rec2.Fields.Count - 1
If rec2.Fields(J).Type = dbText Then
xlSheet.Cells(I, J + 1) = "'" & rec2.Fields(J)
Else
If rec2.Fields(J).Type = dbDate Then
xlSheet.Cells(I, J + 1) = Format(rec2.Fields(J), "dd/mm/yyyy")
Else
xlSheet.Cells(I, J + 1) = rec2.Fields(J)
End If
End If
Next J
I = I + 1
rec2.MoveNext
Wend
' Contrôle de l'existence de chaque répertoire (en général, pour chaque \ trouvé sauf celui de la racine : C:\)
' Ici, on le fait après \TEST
If Dir("C:\Users\gk\Documents\TEST\" & rec1.Fields("Nom"), vbDirectory) = "" Then
MkDir "C:\Users\gk\Documents\TEST\" & rec1.Fields("Nom")
End If
If Dir("C:\Users\gk\Documents\TEST\" & rec1.Fields("Nom") & "\" & Format(DateMin, "YYYYMMDD") & "-" & Format(DateMax, "YYYYMMDD"), vbDirectory) = "" Then
MkDir "C:\Users\gk\Documents\TEST\" & rec1.Fields("Nom") & "\" & Format(DateMin, "YYYYMMDD") & "-" & Format(DateMax, "YYYYMMDD")
End If
' Sauvegarde de la feuille Excel
xlBook.SaveAs "C:\Users\gk\Documents\TEST\" & rec1.Fields("Nom") & "\" & Format(DateMin, "YYYYMMDD") & "-" & Format(DateMax, "YYYYMMDD") & "\" & rec1.Fields("Nom") & "_" & Format(DateMin, "YYYYMMDD") & "-" & Format(DateMax, "YYYYMMDD") & ".xls"
rec1.MoveNext
Wend
xlApp.Quit
rec1.Close
rec2.Close
Set rec1 = Nothing
Set rec2 = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
MsgBox "fin de l'extraction"
End Sub |
Partager