Looping through IDs and outputting locations where they appear in a new sheet?

thechans7

New Member
Joined
Aug 1, 2022
Messages
1
I have a sheet called "IDs" that contains a couple Ids. I want to loop through through each one and find the location/s where each on appears and output the location/s in a new sheet called "Results". How would I do that?

Currently right now, I have a VBA macro that loops through each Id BUT it only puts the last location where each Id was located. So I only get 4 outputs.

VBA Code:
Sub findIds()
Dim i As Long, temp As String
Dim A As Integer
Dim firstAddress As String
Dim sht As Worksheet
Dim c As Range
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
    temp = Cells(i, "A").Value
    For Each sht In ActiveWorkbook.Sheets
    
        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 + ": "
                
            End If
        End With
    Next sht
Next i
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi,

its because you are not changing your output row every time you populate your Result sheet. (you are using i which is just counting the instances on your first sheet).

Try this:
VBA Code:
Sub findIds()
Dim i, output_row As Long, temp As String
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
    
        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" & output_row) = firstAddress + ": " & sht.Name + ": "
                output_row = output_row + 1
            End If
        End With
    Next sht
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,123
Members
453,021
Latest member
Justyna P

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