Return all location in workbook with text is found?

XcelNoobster

New Member
Joined
Jun 7, 2022
Messages
40
So I have a sheet called "IDs" that has a list of Ids and a sheet called "Results". I want to look through each Id, search all the sheets and print all the location where each ID is found in "Results" print them on seperate cells.
So currently right now, my macro is printing only the last location where the Id is found in one cell instead of printing each location on a different cells.

VBA Code:
Sub findIds()
Dim i As Long, temp As String
Dim output_row As Long
Dim A As Integer
Dim firstAddress As String
Dim sht As Worksheet
Dim c As Range

output_row = 1

For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
    temp = Cells(i, "A").Value
    For Each sht In ActiveWorkbook.Sheets
        If sht.Name = "IDs" Then
            'Do Nothing
        Else
         With sht.Cells
        
          Set Rng = .Find(What:=temp, After:=ActiveCell, LookIn:=xlFormulas, _
             LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
             MatchCase:=False, SearchFormat:=False)
        
            If Not Rng Is Nothing Then
                firstAddress = Rng.Address
                Sheets("Results").Range("D" & i) = firstAddress + ": " & sht.Name + ": "
                output_row = output_row + 1
            End If
        End With
        End If
    Next sht
Next i
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try:
VBA Code:
Sub findIds()
    Application.ScreenUpdating = False
    Dim srcRng As Range, rng As Range, sAddr As String, fnd As Range, ws As Worksheet, x As Long: x = 1
    Set srcRng = Sheets("IDs").Range("A2", Sheets("IDs").Range("A" & Rows.Count).End(xlUp))
    For Each rng In srcRng
        For Each ws In Sheets
            If ws.Name <> "IDs" And ws.Name <> "Results" Then
                Set fnd = ws.Cells.Find(rng, LookIn:=xlValues, lookat:=xlPart)
                If Not fnd Is Nothing Then
                    sAddr = fnd.Address
                    Do
                        With Sheets("Results")
                            .Range("D" & x) = fnd.Address + ": " & ws.Name + ": "
                            x = x + 1
                        End With
                        Set fnd = ws.Cells.FindNext(fnd)
                    Loop While fnd.Address <> sAddr
                    sAddr = ""
                End If
            End If
        Next ws
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
another update to your code that may (or may not) do what you want

Rich (BB code):
Option Explicit
Sub findIds()
    Dim IDarr           As Variant, ID As Variant
    Dim sht             As Worksheet, wsID As Worksheet, wsResults As Worksheet
    Dim FirstAddress    As String
    Dim output_row      As Long
    Dim FoundCell       As Range
    Dim WholeOrPart     As XlLookAt
   
    Set wsID = ThisWorkbook.Worksheets("IDs")
    Set wsResults = ThisWorkbook.Worksheets("Results")
   
    IDarr = wsID.Cells(1, 1).Resize(wsID.Cells(wsID.Rows.Count, "A").End(xlUp).Row, 1).Value
   
    'change Find lookAt as required
    WholeOrPart = xlWhole
   
    For Each ID In IDarr
        For Each sht In ThisWorkbook.Worksheets
            If IsError(Application.Match(sht.Name, Array(wsID.Name, wsResults.Name), 0)) Then
           
                Set FoundCell = sht.Cells.Find(What:=ID, LookIn:=xlValues, LookAt:=WholeOrPart, _
                                              SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                              MatchCase:=False, SearchFormat:=False)
               
                If Not FoundCell Is Nothing Then
                    FirstAddress = FoundCell.Address
                    Do
                        output_row = output_row + 1
                        wsResults.Cells(output_row, 4).Value = FoundCell.Address + ": " & sht.Name + ": "
                        Set FoundCell = sht.Cells.Find(FoundCell)
                    Loop Until FoundCell.Address = FirstAddress
                End If
               
            End If
           
            Set FoundCell = Nothing
            FirstAddress = ""
           
        Next sht
    Next ID
End Sub

Solution reads your IDs in to an array which reduces sheet access a little.

note that I changed what Find looks at (whole or part) to xlWhole - you can change it back to part where shown in bold if needed

Hope Helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

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