Find multiple matches in all sheets (solved)

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
Code:
'===================================================
'- FIND MULTIPLE MATCHES IN ALL WORKSHEETS
'- EXAMPLE MACRO TO SEARCH ALL WORKSHEETS
'- AND RETURN DATA TO THE ACTIVE SHEET
'- Gets lookup value from cell A1
'- run this macro from that sheet.
'- change main routine for a different input method
'- and subroutine for different usage of found data
'- Brian Baulsom  May 2005
'=====================================================
Dim ToSheet As Worksheet
Dim ws As Worksheet
Dim MyValue As String
Dim FoundCell As Object
Dim ToRow As Long
Dim FromRow As Long
Dim Counter As Integer
'===================================
'- MAIN ROUTINE
'===================================
Sub FIND_MATCHES_TO_CELL()
    Set ToSheet = ActiveSheet
    ToRow = 1
    MyValue = ToSheet.Cells(ToRow, 1).Value
        search_all_sheets
    MsgBox ("Found " & Counter & " matches.")
End Sub
'= END OF PROCEDURE ======================================
'---------------------------------------------------------
'- subroutine
'---------------------------------------------------------
Private Sub search_all_sheets()
    For Each ws In Worksheets
        If ws.Name <> ActiveSheet.Name Then
        '-look in column A
        With ws.Columns(1).Cells
            '---------------------------------------------
            '- do search
            Set FoundCell = .Find(MyValue, LookIn:=xlValues)
            If Not FoundCell Is Nothing Then ' value found
                FirstAddress = FoundCell.Address
                Do
                    FromRow = FoundCell.Row         ' AMENDED CODE
                    Counter = Counter + 1
                    '-------------------------------------
                    '- transfer values from columns B:C
                    For c = 2 To 3
                       ActiveSheet.Cells(ToRow, c).Value = _
                            ws.Cells(FromRow, c).Value
                    Next
                    '-------------------------------------
                    '- add sheet name to column D
                    ActiveSheet.Cells(ToRow, 4).Value = _
                        ws.Name
                    '-------------------------------------
                    '- find again
                    ToRow = ToRow + 1
                    Set FoundCell = .FindNext(FoundCell)
                    x = FoundCell.Address
 
                Loop While Not FoundCell Is Nothing And _
                    FoundCell.Address <> FirstAddress
                '----------------------------------------
            End If
        End With
        End If
    Next
End Sub
'=============================================
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,215,374
Messages
6,124,569
Members
449,173
Latest member
Kon123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top