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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cnn As ADODB.Connection, CnBud As ADODB.Connection, CnCmd As ADODB.Connection
Dim Rst As ADODB.Recordset, RstBud As ADODB.Recordset, RstCmd As ADODB.Recordset
Dim Fichier As String, Cellule As String, Feuille As String, nomSite As String, FichierBud As String, CellBud As String, FichierCmd As String, CellCmd As String
Dim n As Integer, i As Integer, nbud As Integer, ncmd As Integer
Dim date_debut As Date
nomSite = Range("C2").Value
Feuille = "2019$"
Fichier = "Z:\PBR_LOG\ARIBA_2019\TestVBA\TOTAL\TOTALNord-Est.xlsx"
FichierBud = "Z:\PBR_LOG\ARIBA_2019\TestVBA\Budget\BudgetNord-Est.xlsx"
FichierCmd = "Z:\PBR_LOG\ARIBA_2019\TestVBA\Commandes\CommandesNord-Est.xlsx"
date_debut = Range("B4").Value
If Target.Address = "$C$2" Or Target.Address = "$B$4" Then
Set Cnn = New ADODB.Connection
Cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";Extended Properties='Excel 12.0;HDR=yes'"
Set CnBud = New ADODB.Connection
CnBud.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FichierBud & ";Extended Properties='Excel 12.0;HDR=yes'"
Set CnCmd = New ADODB.Connection
CnCmd.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FichierCmd & ";Extended Properties='Excel 12.0;HDR=yes'"
Set Rst = Cnn.Execute("SELECT count(*) as nb FROM [" & Feuille & "]")
n = Rst("nb")
Cellule = "A3:AE" & n - 2
Set RstBud = CnBud.Execute("SELECT count(*) as nb FROM [" & Feuille & "]")
nbud = RstBud("nb")
CellBud = "A3:I" & nbud + 1
Set RstCmd = CnCmd.Execute("SELECT count(*) as nb FROM [" & Feuille & "]")
ncmd = RstCmd("nb")
CellCmd = "A3:K" & ncmd + 1
Set Rst = New ADODB.Recordset
Set RstBud = New ADODB.Recordset
If Range("C2").Value = "TOTAL" Then
For i = 9 To 45
Set Rst = Cnn.Execute("SELECT SUM(PayéTTC) FROM [" & Feuille & Cellule & "] WHERE P5 = '" & Cells(i, 1) & "' AND (DATE BETWEEN #" & CDate(Range("B4").Value) & "# AND #" & CDate(Range("B5").Value) & "#)")
Cells(i, 3).CopyFromRecordset Rst
Set RstBud = CnBud.Execute("SELECT SUM (Alloué) FROM [" & Feuille & CellBud & "] WHERE P5 = '" & Cells(i, 1) & "'")
Cells(i, 2).CopyFromRecordset RstBud
Set RstCmd = CnCmd.Execute("SELECT SUM(TotalHT) FROM [" & Feuille & CellCmd & "] WHERE P5 = '" & Cells(i, 1) & "'")
Cells(i, 4).CopyFromRecordset RstCmd
Next i
Else
For i = 9 To 45
Set Rst = Cnn.Execute("SELECT SUM(PayéTTC) FROM [" & Feuille & Cellule & "] WHERE SITE = '" & nomSite & "' AND P5 = '" & Cells(i, 1) & "' AND (DATE BETWEEN #" & CDate(Range("B4").Value) & "# AND #" & CDate(Range("B5").Value) & "#)")
Cells(i, 3).CopyFromRecordset Rst
Set RstBud = CnBud.Execute("SELECT SUM(Alloué) FROM [" & Feuille & CellBud & "] WHERE SITE = '" & nomSite & "' AND P5 = '" & Cells(i, 1) & "'")
Cells(i, 2).CopyFromRecordset RstBud
Set RstCmd = CnCmd.Execute("SELECT SUM(TotalHT) FROM [" & Feuille & CellCmd & "] WHERE SITE = '" & nomSite & "' AND P5 = '" & Cells(i, 1) & "'")
Cells(i, 4).CopyFromRecordset RstCmd
Next i
End If
End If
End Sub |
Partager