[VB6 -> VBS]addition

Résolu
serhoe - 14 juin 2013 à 17:16
 serhoe - 17 juin 2013 à 15:02
Salut tout le monde,

J'ai un tableau avec :

cle1 6 2
cle2 2 4
cle3 5 9
cle2 7 3

Et j'aimerais :

cle1 6 2
cle2 9 7
cle3 5 9

C'est à dire garder que des clés uniques et additionner les valeurs des mêmes colonnes si elles sont en plusieurs exemplaires.

Tout fonctionne. A l'exception de l'addition...

For i=0 to Ubound(Tab3,1)
For j=0 to Ubound(Tab4,1)
If Tab4(j,0)=Tab3(i,0) Then
Tab4(j,1)= (Tab4(j,1)+Tab3(i,1))
Tab4(j,2)=Tab4(j,2)+Tab3(i,2)
Tab4(j,3)=Tab4(j,3)+Tab3(i,3)
Tab4(j,4)=Tab4(j,4)+Tab3(i,4)
Tab4(j,5)=Tab4(j,5)+Tab3(i,5)
Tab4(j,6)=Tab4(j,6)+Tab3(i,6)
Tab4(j,7)=Tab4(j,7)+Tab3(i,7)
Tab4(j,8)=Tab4(j,8)+Tab3(i,8)
Tab4(j,9)=Tab4(j,9)+Tab3(i,9)
Tab4(j,10)=Tab4(j,10)+Tab3(i,10)
Tab4(j,11)=Tab4(j,11)+Tab3(i,11)
Tab4(j,12)=Tab4(j,12)+Tab3(i,12)
End If
Next
Next


Dans mon premier exemple, cela me donne ça :

cle1 6 2
cle2 9 7
cle3 27 43

Il ne les additionne pas, mais les mets les uns après les autres...

Avez vous une idée ?

Merci

30 réponses

ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
17 juin 2013 à 11:35
Je te conseille vivement d'ouvrir une autre discussion relative uniquement à la manière la plus agile de dresser ton tableau tab3 à partir de ton fichier csv.
En montrant 2 ou 3 lignes de ce fichier csv
Et nous reviendrons ici lorsque cet aspect-là aura été traité.
Autre discussion à ce seul propos, donc


________________________
Réponse exacte ? => "REPONSE ACCEPTEE" facilitera les recherches.
Pas d'aide en ligne installée ? => ne comptez pas sur moi pour simplement répéter son contenu. Je n'interviendrai que si nécessité de la compléter.
0
Merci pour ton aide ucfoutu, encore un ch'ti problème :

Const ForReading 1, ForWriting 2 
Dim oFso, f
Set oFso = CreateObject("Scripting.FileSystemObject")
Set f = oFso.OpenTextFile("C:\Users\Monique\Downloads\Fichier à importer (1).csv", ForReading)
ln=-1
cl=0
 
while Not f.AtEndOfStream '1ère itération pour définir les limites du tableau
  ln=ln+1 'définition indice lignes
  Tab=Split(f.ReadLine,";")
  If cl < UBound(Tab) Then cl = UBound(Tab) 
Wend
f.Close
'MsgBox "indice lignes : " & ln+1 & "indice colonnes :" & cl+1
 
Dim Tab2()
ReDim Tab2(ln,cl)
Set f = oFso.OpenTextFile("C:\Users\Monique\Downloads\Fichier à importer (1).csv", ForReading)
i=0
while Not f.AtEndOfStream ' 2ème itération pour remplir le tableau Tab2
  Tab = Split(f.ReadLine,";")
  For j = 0 to UBound(Tab)
    Tab2(i,j) = Tab(j)
  Next
  i=i+1
Wend
f.Close

Dim Tab3()
ReDim Tab3(ln-1,cl)

For i=1 to UBound(Tab2,1) ' copie tout de tab2 à tab 3 sauf 1ere ligne marche
For j=0 to Ubound(Tab2,2)'oui		
Tab3(i-1,j)=Tab2(i,j)'oui
Next 
Next

For i=0 to UBound(Tab3,1) ' on copie dans tab3 les clés dans la première colonne de chaque ligne
For j=0 to Ubound(Tab3,2)
Tab3(i,0)=Tab3(i,3)+Tab3(i,5)+Tab3(i,6)+Tab3(i,9)
Next
Next 

For i=0 to Ubound(Tab3,1) ' on décale tous les mois vers la gauche
For j=1 to Ubound(Tab3,2)-20
Tab3(i,j)=Tab3(i,j+20)
Next
Next

Dim fsot, ft   
Set fsot = CreateObject("Scripting.FileSystemObject")
Set ft = fsot.OpenTextFile("C:\Users\Monique\Downloads\lololololololo.txt", 2,true)

dim d,i,t, Tab4
t = Tab3
Set d = CreateObject("Scripting.Dictionary") 
For I = 0 To UBound(Tab3)
  'a d.Keys : b d.items
  if not d.Exists(t(i,0)) then
    d.add Tab3(i,0),i
    t(i,1) = Tab3(i,1)
    t(i,2) = Tab3(i,2)
t(i,3) = Tab3(i,3)
    t(i,4) = Tab3(i,4)
t(i,5) = Tab3(i,5)
    t(i,6) = Tab3(i,6)
t(i,7) = Tab3(i,7)
    t(i,8) = Tab3(i,8)
t(i,9) = Tab3(i,9)
    t(i,10) = Tab3(i,10)
t(i,11) = Tab3(i,11)
    t(i,12) = Tab3(i,12)
   else
    ou = d(t(i,0))
    t(ou,1) = cdbl(Tab3(ou,1)) + cdbl(Tab3(i,1))
    t(ou,2) = cdbl(Tab3(ou,2)) + cdbl(Tab3(i,2))
t(ou,3) = cdbl(Tab3(ou,3)) + cdbl(Tab3(i,3))
t(ou,4) = cdbl(Tab3(ou,4)) + cdbl(Tab3(i,4))
t(ou,5) = cdbl(Tab3(ou,5)) + cdbl(Tab3(i,5))
t(ou,6) = cdbl(Tab3(ou,6)) + cdbl(Tab3(i,6))
t(ou,7) = cdbl(Tab3(ou,7)) + cdbl(Tab3(i,7))
t(ou,8) = cdbl(Tab3(ou,8)) + cdbl(Tab3(i,8))
t(ou,9) = cdbl(Tab3(ou,9)) + cdbl(Tab3(i,9))
t(ou,10) = cdbl(Tab3(ou,10)) + cdbl(Tab3(i,10))
t(ou,11) = cdbl(Tab3(ou,11)) + cdbl(Tab3(i,11))
t(ou,12) = cdbl(Tab3(ou,12)) + cdbl(Tab3(i,12))
    t(i,0) = ""
  end if
next
redim Tab4(d.count-1,13)
n = 0
for i = 0 to ubound(t)
  if t(i,0) <> "" then
    Tab4(n,0) t(i,0) :  Tab4(n,1) t(i,1) :  Tab4(n,2) = t(i,2) : Tab4(n,3) = t(i,3) : Tab4(n,4) = t(i,4) : Tab4(n,5) = t(i,5) : Tab4(n,6) = t(i,6) : Tab4(n,7) = t(i,7) : Tab4(n,8) = t(i,8) : Tab4(n,9) = t(i,9) : Tab4(n,10) = t(i,10) : Tab4(n,11) = t(i,11) : Tab4(n,12) = t(i,12)
    n = n + 1
  end if
next
For i=0 to UBound(Tab4,1) ' copie de tab3 dans le fichier et marche
For j=0 to UBound(Tab4,2)
If m < UBound(Tab4,2) Then 
ft.write(Tab4(i,j) & "	")
m=m+1
Else
ft.write(Tab4(i,j) & vbcrlf)
m=0
End If
Next
Next


Si j'ai trois doublons, exemple :

cle1 2
cle1 3
cle1 8

ça m'affichera :

cle1 10

Il ne prend en compte que le premier et dernier :(
0
ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
17 juin 2013 à 12:39
Je ne serai à nouveau avec toi, serhoe, que lorsque tu auras su sérier les deux problèmes :
- 1) pour faire ton tableau tab3 à partir de ton csv (une discussion)
- 2) l'autre pour traiter ton tab3 (la présente discussion).


________________________
Réponse exacte ? => "REPONSE ACCEPTEE" facilitera les recherches.
Pas d'aide en ligne installée ? => ne comptez pas sur moi pour simplement répéter son contenu. Je n'interviendrai que si nécessité de la compléter.
0
Tab3 fonctionne ucfoutu ! Il est pas super optimisé mais il fonctionne.

Mon problème ici relève du traitement de Tab3 en Tab4 et non pas à partir du CSV
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
17 juin 2013 à 13:11
Mon problème ici relève du traitement de Tab3 en Tab4 et non pas à partir du CSV

Hé non !
Car si tab3 n'est pas dressé correctement (et il me semble que tu t'es évertué à l'inverser inutilement), tout plante !
Tu en veux la démo ? ===>> voilà ===>> je pars de l'exemple répondant à ce que tu as exprimé dans ta demande (hein ...) ===>>
dim tableau(3,2),d,i,t,j,n, resultat
' je "dresse un tableau tab3 (celui de travail) correctement
tableau(0,0) = "clef1"
tableau(0,1) = "6,2"
tableau(0,2) = "2"
tableau(1,0) = "clef2"
tableau(1,1) = "2"
tableau(1,2) = "-4,35"
tableau(2,0) = "clef3"
tableau(2,1) = "5"
tableau(2,2) = "9"
tableau(3,0) = "clef2"
tableau(3,1) = "7"
tableau(3,2) = "3"
t = tableau
Set d = CreateObject("Scripting.Dictionary") 
For I = 0 To UBound(tableau)
  'a d.Keys : b d.items
  if not d.Exists(t(i,0)) then
    d.add tableau(i,0),i
    'for j = 1 to ubound(tableau,2)
    '   t(i,j) = tableau(i,j)
    'next

   else
    ou = d(t(i,0))
    for j = 1 to ubound(tableau,2)
       t(ou,j) = cdbl(tableau(ou,j)) + cdbl(tableau(i,j))
    next
    t(i,0) = ""
  end if
next
redim resultat(d.count-1,2)
n = 0
for i = 0 to ubound(t)
  if t(i,0) <> "" then
    for j = 0 to ubound(tableau,2)
      resultat(n,j) t(i,j) :  resultat(n,j) t(i,j) :  resultat(n,j) = t(i,j)
    next
    n = n + 1
  end if
next

' et je fais maintenant la preuve ====>>
for i = 0 to ubound(resultat)
 msgbox  resultat(i,0)  & "  " &   resultat(i,1) & "  " &   resultat(i,2)
next

alors ?
Maintenant : où tu continues à t'obstiner (et ce sera seul), ou tu réagis comme suggéré (et je serai là) ===>> à toi de voir (on en est à la 3ème page d'une discussion qui n'aurait jamais du excéder UNE page !)!

________________________
Réponse exacte ? => "REPONSE ACCEPTEE" facilitera les recherches.
Pas d'aide en ligne installée ? => ne comptez pas sur moi pour simplement répéter son contenu. Je n'interviendrai que si nécessité de la compléter.
0
Celui ci marche. Car il n'y a que 2 clés identiques au maximum.
De mon côté ça marche aussi avec mon code. Ce qui ne marche pas c'est si t'as au moins 3 clés identiques, exemple :

cle1 2 3
cle1 9 -5
cle1 -3 8

Dans ce cas ça me donnera :

cle1 -1 11

Si j'ai uniquement

cle1 2 3
cle1 9 -5

Ca me donnera :

cle1 11 -2

Tu comprends ? Je m'obstine pas je cherche juste à comprendre
0
Concernant mon Tab3, il s'affiche exactement comme je le souhaite
Bon nombre de lignes et de colonnes !
0
ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
17 juin 2013 à 14:49
ah ! tu peux avoir plus de un seul doublon ? =)== Modif simple et élémentaire ===>>

exemple
dim tableau(3,2),d,i,t,j,n, resultat
tableau(0,0) = "clef1"
tableau(0,1) = "2"
tableau(0,2) = "3"
tableau(1,0) = "clef2"
tableau(1,1) = "2"
tableau(1,2) = "-4,35"
tableau(2,0) = "clef1"
tableau(2,1) = "9"
tableau(2,2) = "-5"
tableau(3,0) = "clef1"
tableau(3,1) = "-3"
tableau(3,2) = "8"
t = tableau
Set d = CreateObject("Scripting.Dictionary") 
For I = 0 To UBound(tableau)
  'a d.Keys : b d.items
  if not d.Exists(t(i,0)) then
    d.add tableau(i,0),i
  else
    ou = d(t(i,0))
    for j = 1 to ubound(tableau,2)
       t(ou,j) = cdbl(t(ou,j)) + cdbl(t(i,j))
    next
    t(i,0) = ""
  end if
next
redim resultat(d.count-1,2)
n = 0
for i = 0 to ubound(t)
  if t(i,0) <> "" then
    for j = 0 to ubound(tableau,2)
      resultat(n,j) t(i,j) :  resultat(n,j) t(i,j) :  resultat(n,j) = t(i,j)
    next
    n = n + 1
  end if
next

'preuve
for i = 0 to ubound(resultat)
 msgbox resultat(i,0)  & "  " &   resultat(i,1) & "  " &   resultat(i,2)
next



________________________
Réponse exacte ? => "REPONSE ACCEPTEE" facilitera les recherches.
Pas d'aide en ligne installée ? => ne comptez pas sur moi pour simplement répéter son contenu. Je n'interviendrai que si nécessité de la compléter.
0
Du coup je suis reparti de mon code, j'ai ajouté les cdbl(expression) que tu avais écrit dans le tien et ça marche comme sur des roulettes.
Dommage ton code était mieux écrit et plus rapide que le mien :( (de 1/2 secondes pour 2200 lignes).

Tab4(j,1)=cdbl(Tab4(j,1))+cdbl(Tab3(i,1))


J'ai maintenant un tout dernier petit problème quelques fois j'ai des problèmes de style :
72.099999999 pour 72.1.

Je poste ça dans un autre sujet, merci ucfoutu, sympa de ta part
0
Const ForReading = 1, ForWriting = 2 
Dim oFso, f
Set oFso = CreateObject("Scripting.FileSystemObject")
Set f = oFso.OpenTextFile("C:\Users\Monique\Downloads\Fichier à importer (1).csv", ForReading)
ln=-1
cl=0
 
while Not f.AtEndOfStream '1ère itération pour définir les limites du tableau
  ln=ln+1 'définition indice lignes
  Tab=Split(f.ReadLine,";")
  If cl < UBound(Tab) Then cl = UBound(Tab) 
Wend
f.Close
'MsgBox "indice lignes : " & ln+1 & "indice colonnes :" & cl+1
 
Dim Tab2()
ReDim Tab2(ln,cl)
Set f = oFso.OpenTextFile("C:\Users\Monique\Downloads\Fichier à importer (1).csv", ForReading)
i=0
while Not f.AtEndOfStream ' 2ème itération pour remplir le tableau Tab2
  Tab = Split(f.ReadLine,";")
  For j = 0 to UBound(Tab)
    Tab2(i,j) = Tab(j)
  Next
  i=i+1
Wend
f.Close

Dim Tab3()
ReDim Tab3(ln-1,cl)

For i=1 to UBound(Tab2,1) ' copie tout de tab2 à tab 3 sauf 1ere ligne marche
For j=0 to Ubound(Tab2,2)'oui		
Tab3(i-1,j)=Tab2(i,j)'oui
Next 
Next

For i=0 to UBound(Tab3,1) ' on copie dans tab3 les clés dans la première colonne de chaque ligne
For j=0 to Ubound(Tab3,2)
Tab3(i,0)=Tab3(i,3)+Tab3(i,5)+Tab3(i,6)+Tab3(i,9)
Next
Next 

For i=0 to Ubound(Tab3,1) ' on décale tous les mois vers la gauche
For j=1 to Ubound(Tab3,2)-20
Tab3(i,j)=Tab3(i,j+20)
Next
Next

Dim fsot, ft   
Set fsot = CreateObject("Scripting.FileSystemObject")
Set ft = fsot.OpenTextFile("C:\Users\Monique\Downloads\lololololololo.txt", 2,true)

dim d,i,t,j,n, Tab4
t = Tab3
Set d = CreateObject("Scripting.Dictionary") 
For I = 0 To UBound(Tab3)
  if not d.Exists(t(i,0)) then
    d.add Tab3(i,0),i
    t(i,1) = Tab3(i,1)
    t(i,2) = Tab3(i,2)
   else
    ou = d(t(i,0))
   
    t(ou,1) = cdbl(t(ou,1)) + cdbl(t(i,1))
    t(ou,2) = cdbl(t(ou,2)) + cdbl(t(i,2))
t(ou,3) = cdbl(t(ou,3)) + cdbl(t(i,3))
t(ou,4) = cdbl(t(ou,4)) + cdbl(t(i,4))
t(ou,5) = cdbl(t(ou,5)) + cdbl(t(i,5))
t(ou,6) = cdbl(t(ou,6)) + cdbl(t(i,6))
t(ou,7) = cdbl(t(ou,7)) + cdbl(t(i,7))
t(ou,8) = cdbl(t(ou,8)) + cdbl(t(i,8))
t(ou,9) = cdbl(t(ou,9)) + cdbl(t(i,9))
t(ou,10) = cdbl(t(ou,10)) + cdbl(t(i,10))
t(ou,11) = cdbl(t(ou,11)) + cdbl(t(i,11))
t(ou,12) = cdbl(t(ou,12)) + cdbl(t(i,12))

    t(i,0) = ""
  end if
next
redim Tab4(d.count-1,2)
n = 0
for i = 0 to ubound(t)
  if t(i,0) <> "" then
    for j = 0 to ubound(Tab3,2)
      Tab4(n,j) t(i,j) :  Tab4(n,j) t(i,j) :  Tab4(n,j) = t(i,j)
    next
    n = n + 1
  end if
next
For i=0 to UBound(Tab4,1) ' copie de tab3 dans le fichier et marche
For j=0 to UBound(Tab4,2)
If m < UBound(Tab4,2) Then 
ft.write(Tab4(i,j) & "	")
m=m+1
Else
ft.write(Tab4(i,j) & vbcrlf)
m=0
End If
Next
Next


J'ai une erreur avec ton code ucfoutu :
"Ligne 85 :Indice en dehors de la place:3"

Cette ligne correspond à la for :

 for j = 0 to ubound(Tab3,2)
      Tab4(n,j) t(i,j) :  Tab4(n,j) t(i,j) :  Tab4(n,j) = t(i,j)
    next
0
Rejoignez-nous