je cherche a simplifier la vie de certains utilisateurs.
ce que je veux: avoir une fonction ne necesitant qu'un paramettre et randant le resultat d'une recherchex....
j'ai essayé de créer ma fonction : fctn_Prix
je dois être aveugle aujourd'hui.... j'y arrive pas...
// modification: j'ai besoin de recherchex, car je vais effectuer des recherches que recherchev ne peut effectuer. L'exemple joint est une simplification//
Merinos
Pièces jointes
test functions.xlsm
16.1 KB · Affichages: 4
Dernière édition:
Solution
C
Compte Supprimé 979
Bonjour Merinos,
La fonction RechercheX() ou en VBA XLOOKUP() retourne une matrice ou plage
Donc le retour ne peut pas être un String
En variant ça fonctionne
VB:
Public Function Fctn_Prix(MaChaine As String) As Variant
Fctn_Prix = Application.XLookup(MaChaine, Range("Table1[fruit]"), Range("Table1[prix]"), "pas trouvé", 0, 1)
End Function
Ceci dit, une simple RechercheV() suffit dans ce cas
Code:
Public Function Fctn_Prix2(MaChaine As String)
Fctn_Prix2 = Application.VLookup(MaChaine, Range("Table1[#All]"), 2, False)
End Function
La fonction RechercheX() ou en VBA XLOOKUP() retourne une matrice ou plage
Donc le retour ne peut pas être un String
En variant ça fonctionne
VB:
Public Function Fctn_Prix(MaChaine As String) As Variant
Fctn_Prix = Application.XLookup(MaChaine, Range("Table1[fruit]"), Range("Table1[prix]"), "pas trouvé", 0, 1)
End Function
Ceci dit, une simple RechercheV() suffit dans ce cas
Code:
Public Function Fctn_Prix2(MaChaine As String)
Fctn_Prix2 = Application.VLookup(MaChaine, Range("Table1[#All]"), 2, False)
End Function
Public Function fctn_Prix(MaChaine As String) As String
Dim result As String
result = Application.WorksheetFunction.XLookup(MaChaine, Range("Table1[fruit]"), Range("Table1[prix]"))
fctn_Prix = result
End Function
On peut améliorer celle de RechercheV() qui fonctionne pareil mais est plus longue
VB:
Public Function Fctn_Prix2(MaChaine As String) As Variant
Dim Result As Variant
On Error Resume Next
Result = Application.VLookup(MaChaine, Range("Table1[#All]"), 2, False)
If Result = 0 Then
Fctn_Prix2 = "Pas trouvé"
Else
Fctn_Prix2 = Result
End If
On Error GoTo 0
End Function
On peut améliorer celle de RechercheV() qui fonctionne pareil mais est plus longue
VB:
Public Function Fctn_Prix2(MaChaine As String) As Variant
Dim Result As Variant
On Error Resume Next
Result = Application.VLookup(MaChaine, Range("Table1[#All]"), 2, False)
If Result = 0 Then
Fctn_Prix2 = "Pas trouvé"
Else
Fctn_Prix2 = Result
End If
On Error GoTo 0
End Function
Désolé mais vlookup ne travaille comme xlookup que si le 5° parametre est "1" ...
Ici je vais devoir mettre "2" ... ce qui permet de rechercher "Bern*" et de retrouver "Bernard" ou "Bernadette".
dans mon cas précis, j'ai le numero de serie mais pas la marque. donc "478478478" retrouve "478478478VOLVO".
Mais comme tu m'as donné la gramais de vlookup, j'ai pu extrapoller .
mon proble était au niveau de "Range("Table1[#All]")"