VBA - Find Method with Multiple Returns
Solved
Mouftie
Posted messages
234
Status
Member
-
Patrice33740 Posted messages 8400 Registration date Status Member Last intervention -
Patrice33740 Posted messages 8400 Registration date Status Member Last intervention -
Hello,
I read your article on multiple returns with great interest:
https://www.commentcamarche.net/faq/18696-vba-recherche-find-avec-retour-multiple
However, I would like to return the information from a cell, rather than the address of the found cell.
I am not very familiar with virtual tables; how can I translate Tb(i) (which is a reference in the form $Col$Lg) into Cells(1, Tb(i)).
In other words, how can I retrieve the row numbers in Tb(i).
Thank you for your help.
I read your article on multiple returns with great interest:
https://www.commentcamarche.net/faq/18696-vba-recherche-find-avec-retour-multiple
However, I would like to return the information from a cell, rather than the address of the found cell.
I am not very familiar with virtual tables; how can I translate Tb(i) (which is a reference in the form $Col$Lg) into Cells(1, Tb(i)).
In other words, how can I retrieve the row numbers in Tb(i).
Thank you for your help.
8 answers
-
Hello,
Like this, adapting FindRech as needed:Sub RechMulti() Dim R As Long, TB() Dim i As Integer, Rch As String Rch = 22 'for example With Sheets("Feuil1") .Columns(7).ClearContents ' for results R = RechFind(Rch, .Range("B1:E500"), 2, TB()) If R > 0 Then .Range("G1") = R & " Occurrences found for: " & Rch .Range("G2").Resize(R, 1) = Application.Transpose(TB) Else MsgBox "Not found", , "Search" End If End With End Sub Function RechFind(ByVal Cle$, ByVal Plage As Range, _ ByVal Col&, ByRef TBval()) As Long 'Returns all the values found in the search 'Cle = searched value 'Plage = range to traverse. 'Col = column containing the values to return. 'TBval = Array returning the values from column Col. Dim Cherche, Ix As Long, Adr With Plage Set Cherche = .Find(Cle, , xlValues, xlWhole) If Not Cherche Is Nothing Then Adr = Cherche.Address Do ReDim Preserve TBval(Ix) TBval(Ix) = Plage.Parent.Cells(Cherche.Row, Col) Set Cherche = .FindNext(Cherche) Ix = Ix + 1 Loop While Not Cherche Is Nothing And Cherche.Address <> Adr End If End With 'number of occurrence(s) found, Return 0 if no occurrence RechFind = Ix Set Cherche = Nothing 'Free the memory occupied by the object. End Function
--
Best regards
Patrice