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
| Sub import()
Annee = UserForm1.ComboBox1
Reponse = MsgBox("Voulez vous charger les données du fichier pour l'année " & Annee & "?", vbQuestion + vbYesNo)
If Reponse = vbYes Then
Dim Cn As ADODB.Connection
Dim oCat As ADOX.Catalog
Dim Fichier As Variant
Dim Feuille As ADOX.Table
Dim Rst As ADODB.Recordset
Dim texte_SQL As String
Dim Ar() As String, i As Long
Fichier = Application.GetOpenFilename("Fichier Excel, *.csv;*.xlsx")
If Fichier = False Then Exit Sub
Set Cn = New ADODB.Connection
Set oCat = New ADOX.Catalog
'Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Fichier & ";Extended Properties=Excel 8.0;"
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
Set oCat.ActiveConnection = Cn
For Each Feuille In oCat.Tables
i = i + 1
ReDim Preserve Ar(i)
Ar(i) = Feuille.Name
Next Feuille
texte_SQL = "SELECT Annee,Semaine,Structure_Utilisateu FROM [" & Ar(1) & "]"
Set Rst = New ADODB.Recordset
Set Rst = Cn.Execute(texte_SQL)
Feuil3.Range("CM4").CopyFromRecordset Rst
Set Feuille = Nothing
Set oCat = Nothing
Cn.Close
Set Cn = Nothing
MsgBox "Données importées"
Else
MsgBox "Vous n'avez exporté aucun fichier"
End If
End Sub |
Partager