FIND MULTIPLE MATCHES IN ALL WORKSHEETS (SOLVED)

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
Some more basic code for a common problem.
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
                FromRow = FoundCell.Row
                Do
                    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)
                Loop While Not FoundCell Is Nothing And _
                    FoundCell.Address <> FirstAddress
                '----------------------------------------
            End If
        End With
        End If
    Next
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,

I am Puneet from India. I have a problem can you please help me in sorting it out.

I have a file (example.xls) with 8 sheets (sheet1, sheet2, sheet3 and so on).
All the sheets have thousands of entries. I need a solution that if there is a text entered in any cell of column A (say A151), macro should search for its duplicate value in all the 8 sheets (Actually it should match all the entries of column A with entire 8 sheet), and should highlight all the duplicates in each sheet with a particular colour (May be with Blue).

Is there a possible soluton to this problem.

Regards
Puneet Jha
 
Upvote 0
Some more basic code for a common problem.
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
                FromRow = FoundCell.Row
                Do
                    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)
                Loop While Not FoundCell Is Nothing And _
                    FoundCell.Address <> FirstAddress
                '----------------------------------------
            End If
        End With
        End If
    Next
End Sub

Hi Brian,

Love this macro. Thank you. But it has an issue. It doesn't return the correct rows. It will find all the occurrences of myvalue but it repeats the first find for the return values. So if it find the first value in row 4 and the next value in row 10, it will return the values from row 4 twice and never return the values from row 10.

Anybody have any ideas how to fix this?
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,363
Members
449,155
Latest member
ravioli44

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