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
| Option Explicit
Public Sub Traitement()
Dim intCol As Integer, intRow As Integer, i As Integer, j As Integer
Dim strfile As String, strcon As String, strSQL As String
Dim cn As Object, rs As Object
Dim tabdonnees As Variant
With Worksheets("Feuil1")
intCol = .Cells(1, .Cells.Columns.Count).End(xlToLeft).Column
intRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range(.[A1], .Cells(1, intCol)).Copy Worksheets("Feuil2").Range("A1")
ActiveWorkbook.Names.Add Name:="tbldonnees", RefersTo:=.Range(.[A1], .Cells(intRow, intCol))
End With
strfile = ActiveWorkbook.FullName
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strfile _
& ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strcon
'Requete SQL
strSQL = "SELECT * FROM tbldonnees WHERE E='9422' OR E='9423'"
'Ouverture de la requete
rs.Open strSQL, cn
'Recuperation de la requète
tabdonnees = rs.getrows()
With Worksheets("Feuil2")
.Cells.Clear
For i = 0 To UBound(tabdonnees, 2)
For j = 0 To UBound(tabdonnees, 1)
.Cells(i + 2, j + 1) = tabdonnees(j, i)
Next j
Next i
End With
ActiveWorkbook.Names("tbldonnees").Delete
End Sub |
Partager