Recherche Excel

Résolu
simlan Messages postés 4 Date d'inscription lundi 10 mai 2004 Statut Membre Dernière intervention 24 avril 2006 - 23 avril 2006 à 11:01
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 - 24 avril 2006 à 17:55
Bonjour,


J'ai besoin d'aide concernant une recherche sous excel.


J'ai 2 fichiers. Le premier contient une date de création; un code article; un fournisseur et un tarif et fait environ 3000 lignes. Le deuxième contient un code article; un fournisseur et un tarif fait environ 200 lignes et est crée une fois par semaine.
Je dois mettre à jour le premier fichier à l'aide du deuxième. Si un code article du deuxième fichier n'existe pas dans le premier, je dois rajouter une ligne contenant la date du jour; le code article et le fournisseur à la fin du premier fichier. Par contre si ce code article existe, je dois rajouter le nouveau fournisseur et le nouveau prix à la suite de l'ancien fournisseur et de l'ancien prix sur la même ligne dans le premier fichier.
Je débute en programmation VBA et j'ai du mal à trouver quelle fonction utiliser.
Quelqu'un peut m'aider?


Simon

7 réponses

mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
24 avril 2006 à 17:55
De rien, tu as bien adapté le code à tes besoins et c'est le principal.

@ ++
Mortalino
3
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
23 avril 2006 à 12:02
Salut Simon,
essaie ce code :

Sub ChercheCodeArticle ()
Dim PLVfichier1 As Long, PLVfichier2 As Long 'PLV veut dire 1ere ligne vide
Dim CountTableau as Long

PLVfichier1 = Columns(1).Find ("", [A1], , , xlByRow, xlNext).Row

WorkBooks.Open ("C:\chemin\nomfichier2.xls")
Sheets("nomOnglet").Select
PLVfichier2 = Columns(1).Find ("", [A1], , , xlByRow, xlNext).Row
CountTableau = PLVfichier2 - 1

Dim CodeArcticle (1 To CountTableau) As String, CodeExiste As Boolean
Dim z As Long, i As Long, j As Long
For i = 1 To CountTableau
CodeArticle(i) = Cells(i, 1).Value
Next i

WorkBooks("nomfichier1.xls").Activate
z = 1
SautX:
CodeExiste = False
For j = 1 To PLVfichier1
If Cells(j, 2).Value CodeArticle(z) Then CodeExiste True
Next j
If CodeExiste = False Then
WorkBooks("nomfichier2.xls").Activate
Rows(z).Copy
WorkBooks("nomfichier2.xls").Activate
Cells(PLVfichier1, 2.Select
Selection.Paste
Cells(PLVfichier1, 1).Value = Date
PLVfichier1 = PLVfichier1 + 1
End If
z = z + 1
If z = PLVfichier1 then GoTo SautY
GotoSautX
SautY:
Application.DisplayAlerts = False
WorkBooks("nomfichier2.xls").Close
Application.DisplayAlerts = True
End Sub

Voilà, en espérant que ce soit nickel.

@ ++
Mortalino
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
23 avril 2006 à 12:05
en relisant j'ai vu 2 erreurs de ma part :

If CodeExiste = False Then
WorkBooks("nomfichier2.xls").Activate
Rows(z).Copy
WorkBooks("nomfichier2.xls").Activate (c pas nomfichier2 mais nomfichier1)
Cells(PLVfichier1, 2.Select (avant .select, j'ai omis la parenthèse)
Selection.Paste
Cells(PLVfichier1, 1).Value = Date
PLVfichier1 = PLVfichier1 + 1
End If

Désolé
0
simlan Messages postés 4 Date d'inscription lundi 10 mai 2004 Statut Membre Dernière intervention 24 avril 2006
23 avril 2006 à 13:19
Merci beaucoup pour ton aide.

J'ai testé le code et a chaque fois il me dit qu'une constante est requise sur CountTableau. Lorsque je remplace CountTableau par un nombre, je recois "sub ou function non defini" sur "CodeArticle(i) ="

Ca te parle?
0

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

Posez votre question
simlan Messages postés 4 Date d'inscription lundi 10 mai 2004 Statut Membre Dernière intervention 24 avril 2006
23 avril 2006 à 13:49
J'ai trouvé pour le le pbe sur code article, un problème d'orthographe. Par contre il me demande tjs une constante requise pour CountTableau.
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
24 avril 2006 à 08:09
Essaie en remplacant les PLV par :

PLVfichier1 = Range("A1").End(xlDown).Row + 1
PLVfichier2 = Range("A1").End(xlDown).Row + 1

J'avais déjà des probleme avec.

@ ++

Mortalino
0
simlan Messages postés 4 Date d'inscription lundi 10 mai 2004 Statut Membre Dernière intervention 24 avril 2006
24 avril 2006 à 16:22
En fait j'y est passé mon après midi hier et voici ce que j'ai fait:

<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />




Sub ChercheCodeArticle()


Dim PLVfichier1 As Long, PLVfichier2 As Long 'PLV veut dire 1ere ligne vide


Dim Demand_Fic2 As String


Dim Received As String


Dim Title_Fic2 As String


Dim ISBN_Fic2 As String


Dim CodeArt_Fic2 As String


Dim ReleaseDate_Fic2 As String


Dim CutOffDate_Fic2 As String


Dim Type_Fic2 As String


Dim Editor_Fic2 As String


Dim PurchasePrice_Fic2 As Single


Dim Pays_Fic2 As String


Dim Infos_Fic2 As String


Dim Ident_Fic2 As String


Dim Qty_Fic2 As String


Dim OffersFrom1_Fic2 As String


Dim Price1_Fic2 As Single





Sheets("Feuil1").Select


'Calcul du nombre de ligne pour le Fichier Principal


PLVfichier1 = Range("C65536").End(xlUp).Row


'Calcul du nombre de ligne pour le Fichier de Mise à Jour


Workbooks.Open ("C:\chemin\maj.xls")


Sheets("Feuil1").Select


PLVfichier2 = Range("C65536").End(xlUp).Row





Dim CodeExiste As Boolean


Dim I As Long 'ligne du fichier MAJ


Dim j As Long 'ligne du fichier Principal


Dim k As Long 'colone tarif du fichier Principal





'Boucle sur le fichier maj


For I = 2 To PLVfichier2


k = 19


CodeExiste = False





Workbooks("maj.xls").Activate


Demand_Fic2 = Cells(I, 1).Value


Received = Cells(I, 2).Value


Title_Fic2 = Cells(I, 3).Value


ISBN_Fic2 = Cells(I, 4).Value


CodeArt_Fic2 = Cells(I, 5).Value


ReleaseDate_Fic2 = Cells(I, 6).Value


CutOffDate_Fic2 = Cells(I, 7).Value


Type_Fic2 = Cells(I, 8).Value


Editor_Fic2 = Cells(I, 9).Value


PurchasePrice_Fic2 = Cells(I, 10).Value


Pays_Fic2 = Cells(I, 11).Value


Infos_Fic2 = Cells(I, 12).Value


Ident_Fic2 = Cells(I, 13).Value


Qty_Fic2 = Cells(I, 14).Value


OffersFrom1_Fic2 = Cells(I, 15).Value


Price1_Fic2 = Cells(I, 10).Value


Receiv1_Fic2 = Cells(I, 2).Value


FrBelg1_Fic2 = Cells(I, 11).Value


Workbooks("modele.xls").Activate



'boucle sur le fichier Principal


For j = 2 To PLVfichier1


If Cells(j, 4).Value = ISBN_Fic2 Then 'On teste si l'enregistrement du Fichier MAJ existe dans le Fichier Principal


CodeExiste = True 'Si oui, on rajoute uniquement le tarif, By, la date de réception et le pays emetteur


Do While Cells(j, k) <> "" 'dans la première cellule OffersFrom vide.


k = k + 4


<?xml:namespace prefix st1 ns "urn:schemas-microsoft-com:office:smarttags" /><st1:place w:st="on">Loop</st1:place>


Cells(j, k) = OffersFrom1_Fic2


Cells(j, k + 1) = PurchasePrice_Fic2


Cells(j, k + 2) = Received


Cells(j, k + 3) = Pays_Fic2


If PurchasePrice_Fic2 < Cells(j, 47).Value Then 'on compare le tarif avec la case Meilleur Prix, si il est plus petit


Cells(j, 47).Value = PurchasePrice_Fic2 'on remplace le tarif BestPrice et le contenu de la case By


Cells(j, 48).Value = OffersFrom1_Fic2


End If





End If





Next j


If CodeExiste = False Then 'Si l'enregistrement n'existe pas, on rajoute une ligne au fichier principal


PLVfichier1 = PLVfichier1 + 1


Workbooks("modele.xls").Activate


Cells(PLVfichier1, 1).Value = Demand_Fic2


Cells(PLVfichier1, 2).Value = Received


Cells(PLVfichier1, 3).Value = Title_Fic2


Cells(PLVfichier1, 4).Value = ISBN_Fic2


Cells(PLVfichier1, 5).Value = CodeArt_Fic2


Cells(PLVfichier1, 6).Value = ReleaseDate_Fic2


Cells(PLVfichier1, 7).Value = CutOffDate_Fic2


Cells(PLVfichier1, 8).Value = Type_Fic2


Cells(PLVfichier1, 9).Value = Editor_Fic2


Cells(PLVfichier1, 10).Value = PurchasePrice_Fic2


Cells(PLVfichier1, 11).Value = Pays_Fic2


Cells(PLVfichier1, 12).Value = Infos_Fic2


Cells(PLVfichier1, 13).Value = Ident_Fic2


Cells(PLVfichier1, 14).Value = Qty_Fic2


Cells(PLVfichier1, 15).Value = OffersFrom1_Fic2


Cells(PLVfichier1, 16).Value = Price1_Fic2


Cells(PLVfichier1, 17).Value = Receiv1_Fic2


Cells(PLVfichier1, 18).Value = FrBelg1_Fic2


Cells(PLVfichier1, 47).Value = Price1_Fic2 'Comme c'est le premier enregistrement, c'est obligatoirement le meilleur prix


Cells(PLVfichier1, 48).Value = OffersFrom1_Fic2


End If





Next I





End Sub



Voila, ca fonctionne pour le momen parfaitement .



Vraiment merci pour ton aide, ca m'a donné un bon départ.

A+
Simlan
0
Rejoignez-nous