Sub testAdO()
Dim fichier As String, nomfeuille As String,tbl
fichier = ThisWorkbook.Path & "\base.xlsx"
nomfeuille = "Feuil1"
tbl = GetTableOnClosedFich3([A1:C10], fichier, nomfeuille, False)
[A1].Resize(UBound(tbl), UBound(tbl, 2)) = tbl
End Sub
Function GetTableOnClosedFich3(Plage, fichier, nomfeuille, Optional header As Boolean = False)
Dim Cn As Object, texte_SQL$, rst As Object, tbl, head$
Set Cn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.RecordSet")
head = Array("NO", "YES")(Abs(header))
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & fichier & ";Extended Properties=""Excel 12.0;HDR=" & head & ";IMEX=1"";"
' la requête.Attention!!!!!!! à ne pas oublier le symbole "$" après le nom de la feuille.
texte_SQL = "SELECT * FROM [" & nomfeuille & "$" & Plage.Address(0, 0) & "]"
Set rst = Cn.Execute(texte_SQL)
GetTableOnClosedFich3 = Application.Transpose(rst.GetRows)
Cn.Close: Set Cn = Nothing: Set rst = Nothing '--- Fermeture connexion ---et vidage des object dans la memoire
End Function