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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
Sub etat2()
Dim Cn As ADODB.Connection
Dim Fichier As String
Dim NomFeuille As String, texte_SQL1 As String, texte_SQL2 As String
Dim Rst1 As ADODB.Recordset
Dim Rst2 As ADODB.Recordset
Fichier = "D:\code.xlsm"
NomFeuille = "Feuil4$"
Set Cn = New ADODB.Connection
With Cn
.Provider = "Microsoft.Jet.OLEDB.2.8"
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
& Fichier & ";Extended Properties=""Excel 12.0;HDR=YES;"""
.Open
End With
m = 3
Do
m = m + 1
Set design = Worksheets("Feuil1").Cells(m, 5)
Set quantiteng = Worksheets("Feuil1").Cells(m, 7)
Set prixun = Worksheets("Feuil1").Cells(m, 8)
Set Total1 = Worksheets("Feuil1").Cells(m, 9)
c = 3
Do
c = c + 1
Set article = Worksheets("Feuil2").Cells(c, 1)
Set quantite = Worksheets("Feuil2").Cells(c, 2)
Set prixmoy = Worksheets("Feuil2").Cells(c, 3)
Set Total2 = Worksheets("Feuil2").Cells(c, 4)
Set poids = Worksheets("Feuil2").Cells(c, 5)
If article.Value = design.Value Then Exit Do
Loop Until article.Value = ""
If (design.Value <> article.Value And article.Value = "") Then
article.Value = design.Value
quant = WorksheetFunction.SumIf(Worksheets("Feuil1").Range("E4:E100"), article.Value, Worksheets("Feuil1").Range("G4:G100"))
quantite.Value = quant
If quant <> 0 Then
prix1 = Evaluate("sumproduct((Feuil1!E4:E100=" & article.Address & ")*(Feuil1!G4:G100*Feuil1!H4:H100))")
prixmoy.Value = Round(prix1 / quantite, 3)
Total2.Value = WorksheetFunction.Round(prixmoy.Value * quantite.Value, 0)
Worksheets("Feuil2").Cells(c, 1).Borders.LineStyle = xlContinuous
Worksheets("Feuil2").Cells(c, 2).Borders.LineStyle = xlContinuous
Worksheets("Feuil2").Cells(c, 3).Borders.LineStyle = xlContinuous
Worksheets("Feuil2").Cells(c, 4).Borders.LineStyle = xlContinuous
Worksheets("Feuil2").Cells(c, 5).Borders.LineStyle = xlContinuous
i = 2
Do
i = i + 1
article3 = "B" & i
poids3 = "G" & i
texte_SQL1 = "SELECT * FROM [" & NomFeuille & article3 & ":" & article3 & "]"
texte_SQL2 = "SELECT * FROM [" & NomFeuille & poids3 & ":" & poids3 & "]"
Set Rst1 = New ADODB.Recordset
Set Rst2 = New ADODB.Recordset
Set Rst1 = Cn.Execute(texte_SQL1)
Set Rst2 = Cn.Execute(texte_SQL2)
'Rst1.Open texte_SQL1, Cn
'Rst2.Open texte_SQL2, Cn
Valeur = Rst1.Fields(0).Name
Valeur2 = Rst2.Fields(0).Name
If article.Value = Valeur Then Exit Do
Loop Until Valeur = "F1"
If article.Value = Valeur Then poidsuni = Valeur2
If Valeur2 <> "F1" Then poids.Value = Round(poidsuni * quantite.Value, 0)
If Valeur2 = "F1" Then poids.Value = "Aucune indication"
If Valeur = "F1" Then poids.Value = "Aucune indication"
End If
If quant = 0 Then
article.Clear
quantite.Clear
Total2.Clear
poids.Clear
End If
End If
If (design.Value <> article.Value And article.Value <> "") Then
Worksheets("Feuil2").Cells(c + 1, 1).Value = design.Value
quant1 = WorksheetFunction.SumIf(Worksheets("Feuil1").Range("E4:E100"), Worksheets("Feuil2").Cells(c + 1, 1).Value, Worksheets("Feuil1").Range("G4:G100"))
Worksheets("Feuil2").Cells(c + 1, 2).Value = quant1
End If
Loop Until design.Value = ""
Cn.Close
Set Cn = Nothing
End Sub |