Bonjour à tous,
J'ai un problème avec mon code VBA, que je comprend pas.
voici mon code VBA
Le probleme se situe à ce niveau:
Ma requete SQL
Lorsque j’exécute ma macro, les donnée ne s’inscrit pas dans mon feuille data2 excel. si je change
en
j'ai bien le résultat qui s'inscrit.
La requete SQL est OK ('voici le resultalt )
Autre élément si dans ma requete SQL je met
en dernier dans le select ==> Dans mon fichier excel il inscrit bien toutes donnée dans les colonnes sauf la derniere ??!!!
Pourriez-vous m'aiguillez pour résoudre ce problème
Merci
guigui69
J'ai un problème avec mon code VBA, que je comprend pas.
voici mon code VBA
Code:
Sub Macro1()
Dim valcel As String
Dim valce2 As String
valcel = Format(Excel.Range("Informations!B3").Value, "yyyy-mm-dd")
valce2 = Format(Excel.Range("Informations!C3").Value, "yyyy-mm-dd")
valceln1 = Format(DateAdd("yyyy", -1, valcel), "yyyy-mm-dd")
valce2n2 = Format(DateAdd("yyyy", -1, valce2), "yyyy-mm-dd")
'MsgBox (valceln1 & "---" & valce2n2)
chemin = ActiveWorkbook.Path
Dim cnBat As ADODB.Connection
Set cnBat = New ADODB.Connection
cnBat.Open "DRIVER={MySQL ODBC 5.2 ANSI Driver};" & "SERVER=ggggggg;" & "DATABASE=yyyyy;" & "USER=ggggg;" & "PASSWORD=ggggg;" & "Option=3"
cnBat.CommandTimeout = 0
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Worksheets("data1").Range("A2:DK5000").ClearContents
' Worksheets("BD-n1").Range("A2:DK5000").ClearContents
' Worksheets("BDvo").Range("A2:DK5000").ClearContents
' Worksheets("BDvo-n1").Range("A2:DK5000").ClearContents
Dim rsBat As ADODB.Recordset
Set rsBat = New ADODB.Recordset
With rsBat
.ActiveConnection = cnBat
'MsgBox ("Societe:" & rsBat("emp") & "Point de vente" & rsBat("puntoventa"))
Set f = fso.OpenTextFile(chemin & "\sql\commande-v2.sql", ForReading)
une_variable = Replace(f.ReadAll, "date_debut", valcel)
une_variable = Replace(une_variable, "date_fin", valce2)
une_variable = Replace(une_variable, "nfindate", valce2n2)
une_variable = Replace(une_variable, "ndebutdate", valceln1)
f.Close
.Open une_variable
Worksheets("Data1").Range("A2").CopyFromRecordset rsBat
.Close
End With
Set f2 = fso.OpenTextFile(chemin & "\test.txt", ForWriting, True)
f2.write (une_variable)
f2.Close
'' '-vendeur
Set f3 = fso.OpenTextFile(chemin & "\sql\commande-par-vendeur.sql", ForReading)
une_variable3 = Replace(f3.ReadAll, "date_debut", valcel)
une_variable3 = Replace(une_variable3, "date_fin", valce2)
'une_variable3 = Replace(une_variable3, "nfindate", valce2n2)
'une_variable3 = Replace(une_variable3, "ndebutdate", valceln1)
f3.Close
Worksheets("Data2").Range("A2:dk65000").ClearContents
Dim rsBati1 As ADODB.Recordset
Set rsBati1 = New ADODB.Recordset
With rsBati1
.ActiveConnection = cnBat
.Open une_variable3
For Count = 0 To rsBati1.Fields.Count - 1
MsgBox rsBati1.Fields.Item(Count).Value
Next
Worksheets("Data2").Range("A2").CopyFromRecordset rsBati1
.Close
End With
Set f2 = fso.OpenTextFile(chemin & "\test.txt", ForWriting, True)
f2.write (une_variable3)
f2.Close
'' '-vendeurn-1
Set f4 = fso.OpenTextFile(chemin & "\sql\commande-par-vendeur.sql", ForReading)
une_variable4 = Replace(f4.ReadAll, "date_debut", valceln1)
une_variable4 = Replace(une_variable4, "date_fin", valce2n2)
f4.Close
Worksheets("Data3").Range("A2:dk65000").ClearContents
Dim rsBati12 As ADODB.Recordset
Set rsBati12 = New ADODB.Recordset
With rsBati12
.ActiveConnection = cnBat
.Open une_variable4
Worksheets("Data3").Range("A2").CopyFromRecordset rsBati12
.Close
End With
cnBat.Close
'Worksheets("TCD").PivotTables("Tableau croisé dynamique2").PivotCache.Refresh
Set rsBat = Nothing
Set cnBat = Nothing
End Sub
Le probleme se situe à ce niveau:
Code:
'-vendeur
Set f3 = fso.OpenTextFile(chemin & "\sql\commande-par-vendeur.sql", ForReading)
une_variable3 = Replace(f3.ReadAll, "date_debut", valcel)
une_variable3 = Replace(une_variable3, "date_fin", valce2)
'une_variable3 = Replace(une_variable3, "nfindate", valce2n2)
'une_variable3 = Replace(une_variable3, "ndebutdate", valceln1)
f3.Close
Worksheets("Data2").Range("A2:dk65000").ClearContents
Dim rsBati1 As ADODB.Recordset
Set rsBati1 = New ADODB.Recordset
With rsBati1
.ActiveConnection = cnBat
.Open une_variable3
For Count = 0 To rsBati1.Fields.Count - 1
MsgBox rsBati1.Fields.Item(Count).Value
Next
Worksheets("Data2").Range("A2").CopyFromRecordset rsBati1
.Close
End With
Ma requete SQL
Code:
SELECT v.prenom_vendeur,
year(DateCommande) as annee,
month(DateCommande) as mois,
count(CASE WHEN `DateCommande` BETWEEN "date_debut" AND "date_fin" THEN id_cde END) as cden,
SUM(CASE WHEN `DateCommande` BETWEEN "date_debut" AND "date_fin" THEN Societe END) as societe,
sum(CASE WHEN `DateCommande` BETWEEN "date_debut" AND "date_fin" THEN `Entretien` END) as entretien,
SUM(CASE WHEN `DateCommande` BETWEEN "date_debut" AND "date_fin" THEN IF(`Montant` <> "0", 1, 0) END) as fi,
sum(CASE WHEN `DateCommande` BETWEEN "date_debut" AND "date_fin" THEN `Box` END) as box,
sum(CASE WHEN `DateCommande` BETWEEN "date_debut" AND "date_fin" THEN `CLD` END) as cld
FROM `commande_vn`
INNER JOIN vendeur v on v.id_vendeur=`commande_vn`.vendeur
WHERE (`DateCommande` BETWEEN "date_debut" AND "date_fin")
GROUP BY v.nom_vendeur,v.prenom_vendeur,annee,mois
ORDER BY v.nom_vendeur,v.prenom_vendeur,annee,mois
Lorsque j’exécute ma macro, les donnée ne s’inscrit pas dans mon feuille data2 excel. si je change
Code:
v.prenom_vendeur,
Code:
v.id_vendeur,
La requete SQL est OK ('voici le resultalt )
Autre élément si dans ma requete SQL je met
Code:
v.prenom_vendeur,
Pourriez-vous m'aiguillez pour résoudre ce problème
Merci
guigui69