Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

Traduction formule en VBA

mfaeko

XLDnaute Nouveau
Bonjour à tous,

Pour alléger mon fichier, je souhaite transformer la formule suivante en VBA.
INDEX(Commandes!J:J;EQUIV([@[Bon de commande]];Commandes!L:L;0)

J'ai bien essayé avec ça :
Range("U" & Target.Row) = Application.WorksheetFunction.VLookup(Target.Value, Sheets("Commandes").Range("J:L"), 3, 0)

Ça fonctionne très bien pour une recherche sur le même onglet mais comme ma formule va chercher sur un autre onglet (commandes)et là ça bug.

Avez-vous une idée d'où ça peut venir ?

Merci d'avance
mfaeko
 

Dranreb

XLDnaute Barbatruc
Bonjour.
Je dirais :
VB:
With Worksheets('Commandes")
    Cells(Target.Row, "U").Value = .Cells(WorksheetFunction.Match(Target.Value, .Columns("L"), 0), "J").Value
    End With
Ou pour parer à un éventuel problème :
VB:
Dim L as Long
With Worksheets('Commandes")
    On Error Resume Next: L = WorksheetFunction.Match(Target.Value, .Columns("L"), 0)
    If Err Then L = 0
    On Error GoTo 0
    If L > 0 Then Cells(Target.Row, "U").Value = .Cells(L, "J").Value
    End With
 
Dernière édition:

patricktoulon

XLDnaute Barbatruc
bonsoir
et la gestion erreur du non match alors????
VB:
With Worksheets("Commandes").Cells(Target.Row, "U")
    x = Application.IfError(Application.Match(Target.Value, .Columns("L"), 0), 0)
    .Value = IIf(x = 0, .Value,.Cells(x, "J")

End With

edit: oui ta façon marche aussi Dranreb
 
Dernière édition:

patricktoulon

XLDnaute Barbatruc
re
bonjour @Dranreb
??????????????????????????????????????????????????????????????

mon code ne veut absolument pas dire
MsgBox Cells(Target.Row, "U").Cells(x, "J").Address affiche "$AD&109" et non "$J$100"
quel est l’intérêt de coder ça
MsgBox Cells(Target.Row, "U").Cells(x, "J")
 

patricktoulon

XLDnaute Barbatruc
a oui!! autant pour moi
VB:
With Worksheets("Commandes").Cells(Target.Row, "U")
    x = Application.IfError(Application.Match(Target.Value, .Columns("L"), 0), 0)
    .Value = IIf(x = 0, .Value,.parent.Cells(x, "J")
End With
 

Discussions similaires

Réponses
1
Affichages
678
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…