Sub test_récup_plage()
Dim fichier$, T
fichier = "C:\Users\patrick1\Desktop\Telechar\JournalAux-97.xlsx" 'à adapter
T = GetUserRangeOnClosedFich2(fichier, "A1:k26", "JournalReport", False)
ShDatas.[A1].Resize(UBound(T), UBound(T, 2)) = T
End Sub
'renvoie les valeurs d'une plage de cellules contigües (RnG)
'd'une feuille (Feuille) d'un fichier (fichier) fermé
'le paramètre headerTable indique si la plage a ou non une ligne d'entêtes
Function GetUserRangeOnClosedFich2(fichier As String, RnG As String, Optional Feuille As String = "", Optional headerTable As Boolean = False)
Dim HDR As String, RsTLigne As Integer, RsTCol As Integer
'early binding
'Dim AdConn As ADODB.Connection, AdoComand As ADODB.Command, RsT As ADODB.Recordset
'Set AdConn = New ADODB.Connection
'Set RsT = New ADODB.Recordset
'Set AdoComand = New ADODB.Command
'late binding
Dim AdConn As Object, AdoComand As Object, RsT As Object
Set AdConn = CreateObject("ADODB.Connection")
Set AdoComand = CreateObject("ADODB.Command")
Set RsT = CreateObject("ADODB.Recordset")
HDR = Array("No", "Yes")(Abs(headerTable))
AdConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fichier & ";Extended Properties=""Excel 12.0;HDR=No;IMEX=1;"""
AdoComand.ActiveConnection = AdConn
If Feuille = "" _
Then AdoComand.CommandText = "SELECT * from `" & RnG & "`" _
Else AdoComand.CommandText = "SELECT * from `" & Feuille & "$" & RnG & "`"
RsT.Open AdoComand, , 1, 1
ReDim Arr(1 To RsT.RecordCount, 1 To RsT.Fields.Count)
RsT.MoveFirst
Do While Not RsT.EOF
For RsTLigne = 1 To RsT.RecordCount 'lignes
For RsTCol = 0 To RsT.Fields.Count - 1 'colonnes
Arr(RsTLigne, RsTCol + 1) = RsT.Fields(RsTCol).Value
Next
RsT.MoveNext
Next
Loop
AdConn.Close: Set RsT = Nothing: Set AdoComand = Nothing: Set AdConn = Nothing
GetUserRangeOnClosedFich2 = Arr
End Function