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
|
Sub ImportFromExcelFile()
Dim sheetDest As Worksheet
Dim ImportFileName
Dim cn As Object
Dim rs As Object
Dim strSql As String
Dim r
Dim c As Integer
On Error GoTo lblErr
'Requête pour récuppérer les données du fichier Ecel à importer
strSql = "SELECT * FROM [Feuil1$A5:S10000]"
'Sélectionner le fichier Excel à importer
ImportFileName = Application.GetOpenFilename("Fichier Excel (*.xls), *.xls")
If ImportFileName = False Then Exit Sub
'Se connecter au fichier
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=""" & ImportFileName & """;Extended Properties=Excel 8.0;"
.Open
'Extraire les données
Set rs = .Execute(strSql)
'Copier les données dans l'onglet à partir de la ligne r et la colonne c
Set sheetDest = ThisWorkbook.Worksheets("Feuil1")
Set r = sheetDest.Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If r Is Nothing Then
r = 1
Else
r = r.Row
End If
c = 1
sheetDest.Cells(r, c).CopyFromRecordset rs
rs.Close
.Close
End With
GoTo lblFin
lblErr:
MsgBox Err.Description
lblFin:
Set sheetDest = Nothing
Set rs = Nothing
Set cn = Nothing
End Sub |
Partager