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
| Sub extractionPlageCellules_ClasseursFermes()
Dim Source As Object, Rst As Object, ADOCommand As Object
Dim Fichier As String, Repertoire As String
Dim Cellule As String, Feuille As String
Repertoire = "C:\NomDossier"
Cellule = "B5:G20" 'plage de cellules à extraire
Feuille = "Feuil1$" 'n'oubliez pas d'ajouter $ au nom de la feuille
'Définit le répertoire contenant les classeurs
Fichier = Dir(Repertoire & "\*.xls")
'Boucle sur tous les classeurs fermés du répertoire cible
Do While Fichier <> ""
Set Source = CreateObject("ADODB.Connection")
Source.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Repertoire & "\" & Fichier & _
";Extended Properties=""Excel 8.0;HDR=No;"";"
Set ADOCommand = CreateObject("ADODB.Command")
With ADOCommand
.ActiveConnection = Source
.CommandText = "SELECT * FROM `" & Feuille & Cellule & "`"
End With
Set Rst = CreateObject("ADODB.Recordset")
Rst.Open ADOCommand, , 1, 3 '1=adOpenKeyset, 3=adLockOptimistic
Set Rst = Source.Execute("`" & Feuille & Cellule & "`")
Range("A" & Range("A65536").End(xlUp).Row + 1).CopyFromRecordset Rst
Rst.Close
Source.Close
Set Source = Nothing
Set Rst = Nothing
Set ADOCommand = Nothing
Fichier = Dir
Loop
End Sub |
Partager