Sub test_récup_plage()
Dim fichier$, T
fichier = ThisWorkbook.Path & "\Datas externes.xlsx" 'à adapter
T = GetUserRangeOnClosedFich2(fichier, "$C$11:$C$15;$C20:$C$21;$C$18", "Feuil1", False)
[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
HDR = Array("No", "Yes")(Abs(headerTable))
AdConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fichier & ";Extended Properties=""Excel 12.0;HDR=No;IMEX=1;"""
Dim SQL As String
If Feuille = "" Then
SQL = "SELECT * from `" & RnG & "`"
Else
For Each g In Split(Replace(RnG, "$", "") & ";", ";")
If g <> "" Then
If SQL <> "" Then SQL = SQL & " Union All"
SQL = SQL & " SELECT * from [" & Feuille & "$" & g & IIf(InStr(1, g, ":") = 0, ":" & g, "") & "]"
End If
Next
End If
Dim Rs As Object: Set Rs = CreateObject("ADODB.Recordset")
Rs.Open SQL, AdConn, 1, 1
If Not Rs.EOF Then Arr = Application.Transpose(Rs.GetRows) Else Arr = Array("", "")
Rs.Close: Set Rs = Nothing
GetUserRangeOnClosedFich2 = Arr
End Function