Function recherche : code perdu

sniper2002

XLDnaute Occasionnel
Bonjour

Je souhaite reconstitué un code qui me permetait la création d'une fonction personnalisée avec la synthaxe suivante :

Exemple : nom de la function = toto
Dans Excel la synthaxe doit ressembler à ça :
=toto(plage de critères;critère;plage de valeurs recherchées).

a peu près dans la même synthaxe que "somme.si", sauf que cette fonction permet de faire une recherche dans la colonne des valeurs/textes recherchées et te donne un résultat d'une valeur ou plusieurs valeurs avec une séparation "-" qu'on peut paramètrer dans le code

exemple : Fonction dans la cellule K3 : =toto(A1:A20;K2;B1:B20)
Résultat qui correspond au critère K2 : 10 - 125 - 75

ce fameux code je l'ai récupéré sur internet, le problème c'est que j'ai perdu sa trace, donc si quelqu'un parmi vous à le lien ou il peut reconstituer cette Function, je vous serai reconnaissant pour laide apportée.

merci d'avance
 

Staple1600

XLDnaute Barbatruc
Re : Function recherche : code perdu

Bonjour


A tout hasard?
Est-ce cette fonction de C.Pearson ?
VB:
Function FindAll(SearchRange As Range, _
                FindWhat As Variant, _
               Optional LookIn As XlFindLookIn = xlValues, _
                Optional LookAt As XlLookAt = xlWhole, _
                Optional SearchOrder As XlSearchOrder = xlByRows, _
                Optional MatchCase As Boolean = False, _
                Optional BeginsWith As String = vbNullString, _
                Optional EndsWith As String = vbNullString, _
                Optional BeginEndCompare As VbCompareMethod = vbTextCompare) As Range
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FindAll
' This searches the range specified by SearchRange and returns a Range object
' that contains all the cells in which FindWhat was found. The search parameters to
' this function have the same meaning and effect as they do with the
' Range.Find method. If the value was not found, the function return Nothing. If
' BeginsWith is not an empty string, only those cells that begin with BeginWith
' are included in the result. If EndsWith is not an empty string, only those cells
' that end with EndsWith are included in the result. Note that if a cell contains
' a single word that matches either BeginsWith or EndsWith, it is included in the
' result.  If BeginsWith or EndsWith is not an empty string, the LookAt parameter
' is automatically changed to xlPart. The tests for BeginsWith and EndsWith may be
' case-sensitive by setting BeginEndCompare to vbBinaryCompare. For case-insensitive
' comparisons, set BeginEndCompare to vbTextCompare. If this parameter is omitted,
' it defaults to vbTextCompare. The comparisons for BeginsWith and EndsWith are
' in an OR relationship. That is, if both BeginsWith and EndsWith are provided,
' a match if found if the text begins with BeginsWith OR the text ends with EndsWith.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim FoundCell As Range
Dim FirstFound As Range
Dim LastCell As Range
Dim ResultRange As Range
Dim XLookAt As XlLookAt
Dim Include As Boolean
Dim CompMode As VbCompareMethod
Dim Area As Range
Dim MaxRow As Long
Dim MaxCol As Long
Dim BeginB As Boolean
Dim EndB As Boolean


CompMode = BeginEndCompare
If BeginsWith <> vbNullString Or EndsWith <> vbNullString Then
    XLookAt = xlPart
Else
    XLookAt = LookAt
End If

' this loop in Areas is to find the last cell
' of all the areas. That is, the cell whose row
' and column are greater than or equal to any cell
' in any Area.

For Each Area In SearchRange.Areas
    With Area
        If .Cells(.Cells.Count).Row > MaxRow Then
            MaxRow = .Cells(.Cells.Count).Row
        End If
        If .Cells(.Cells.Count).Column > MaxCol Then
            MaxCol = .Cells(.Cells.Count).Column
        End If
    End With
Next Area
Set LastCell = SearchRange.Worksheet.Cells(MaxRow, MaxCol)

On Error GoTo 0
Set FoundCell = SearchRange.Find(what:=FindWhat, _
        after:=LastCell, _
        LookIn:=LookIn, _
        LookAt:=XLookAt, _
        SearchOrder:=SearchOrder, _
        MatchCase:=MatchCase)

If Not FoundCell Is Nothing Then
    Set FirstFound = FoundCell
    Do Until False ' Loop forever. We'll "Exit Do" when necessary.
        Include = False
        If BeginsWith = vbNullString And EndsWith = vbNullString Then
            Include = True
        Else
            If BeginsWith <> vbNullString Then
                If StrComp(Left(FoundCell.Text, Len(BeginsWith)), BeginsWith, BeginEndCompare) = 0 Then
                    Include = True
                End If
            End If
            If EndsWith <> vbNullString Then
                If StrComp(Right(FoundCell.Text, Len(EndsWith)), EndsWith, BeginEndCompare) = 0 Then
                    Include = True
                End If
            End If
        End If
        If Include = True Then
            If ResultRange Is Nothing Then
                Set ResultRange = FoundCell
            Else
                Set ResultRange = Application.Union(ResultRange, FoundCell)
            End If
        End If
        Set FoundCell = SearchRange.FindNext(after:=FoundCell)
        If (FoundCell Is Nothing) Then
            Exit Do
        End If
        If (FoundCell.Address = FirstFound.Address) Then
            Exit Do
        End If

    Loop
End If
    
Set FindAll = ResultRange

End Function
 

Staple1600

XLDnaute Barbatruc
Re : Function recherche : code perdu

Re


Et là on se rapproche
VB:
Public Function MultiLookUp(Find As Range, InRange As Range, Column As
Integer) As String
For Each Cell In InRange
  If Cell.Value = Find.Value Then
  output = output & Cell.Offset(0, Column - 1).Value & "-"
  End If
  Next
  MultiLookUp = Left(output, Len(output) - 2)
  End Function'source: Dan E.
 

sniper2002

XLDnaute Occasionnel
Re : Function recherche : code perdu

Excellent, merci beaucoup pour l'aide, elle change un peu mais l'utilité est la même.
Cependant comment je peu faire pour que cette macro se charge à l'ouverture d'excel? j'ai essayé de l'ajouter à mes macros complémentaire, mais j'ai message me disant que la macro est non valide,
sachant que j'ai utilisé le format xla et xls
merci pour laide
a+
 

Staple1600

XLDnaute Barbatruc
Re : Function recherche : code perdu

Re


Ce serait plus simple avec un fichier exemple
(car j'ai ^pas trop compris ton exemple avec K2)

Sinon j'ai testé ceci, cela fonctionne mais mal
J'ai mis cette fonction dans un fichier nommé test.xla
Code:
Public Function MultiLookUp(Find As Range, InRange As Range) As String
Dim Cell As Range, output$
With Application.Caller
For Each Cell In InRange
If Cell.Value = Find.Value Then
output = output & Cell & "-"
End If
Next
MultiLookUp = Left(output, Len(output) - 1)
End With
End Function 'source: Dan E.

Ensuite dans un classeur, pour utiliser cette fonction, j'ai saisi en B1 (par exemple)
=test.xla!multilookup(D1;A1:A9)

Cela fonctionne mais ensuite c'est là qu'est le hic (je te laisse le découvrir)
 

Discussions similaires

Membres actuellement en ligne

Statistiques des forums

Discussions
312 581
Messages
2 089 916
Membres
104 306
dernier inscrit
Bouhlal