
Originally Posted by
beyond Excel
Hi Serena. Try with:
Finally, Ive rearranged my Function to work properly, since some values could not be calculated with the script that you'd given me, anyway I greatly appreciate all your help and I have been able to learn some good techniques to continue progressing in this programming environment. Greetings and sure we'll meet again on the forum.
And this is the Function in which Ive been working during a week and finally works very fast and fine as it has to do. I share with all of you because I think is an interesting Function to work with, so someone else could use it for future projects. This is my favorite place in search for Excel doubts, and without doubt I will be aware of all the news that can help me and be useful to me and others users.
Function Triplets(ByVal cela_objectiu As Range, ByVal rang_dades As Range) As Variant
Dim dades_cerca As Range
Dim grup_cerca_valors_A As Object, grup_cerca_valors_B As Object
Dim primer_valor As Long, segon_valor As Long
Set grup_cerca_valors_A = CreateObject("Scripting.Dictionary")
Set grup_cerca_valors_B = CreateObject("Scripting.Dictionary")
primer_valor = cela_objectiu.Offset(0, -2).Value ' Valors de la columna A, dos llocs a l'esquerra d'on cridem la funció
segon_valor = cela_objectiu.Offset(0, -1).Value ' Valors de la columna B, un lloc a l'esquerra d'on cridem la funció.
' realitzem la cerca del primer valor, el de la columna A
For Each dades_cerca In rang_dades.Columns(2).Cells
If dades_cerca.Row > cela_objectiu.Row Then
If dades_cerca.Value = primer_valor Then
grup_cerca_valors_A(dades_cerca.Offset(0, -1).Value) = True ' Afegim el valor trobat en la columna A al set de valors trobats pel grup A
End If
End If
Next dades_cerca
' realitzem la cerca del segon valor, el de la columna B
For Each dades_cerca In rang_dades.Columns(2).Cells
If dades_cerca.Row > cela_objectiu.Row Then
If dades_cerca.Value = segon_valor Then
grup_cerca_valors_B(dades_cerca.Offset(0, -1).Value) = True ' Afegim el valor trobat en la columna A al set de valors trobats pel grup B
End If
End If
Next dades_cerca
' busquem les possibles coincidencies entre els dos grups de cerca el grup pels valors d'A i el grup pels valors de B
For Each nombre In grup_cerca_valors_A.Keys
If grup_cerca_valors_B.Exists(nombre) Then
Triplets = nombre ' retorna el valor numèric si hi ha una coincidència
Exit Function
End If
Next nombre
Triplets = "no_trobat" ' Si no es troba cap coincidència, retorna el text "no_trobat"
End Function
Bookmarks