Macro to Loop Through List of Names and Find Worksheet with Same Name

andybason

Board Regular
Joined
Jan 7, 2012
Messages
217
Office Version
  1. 2016
Hello

I have a worksheet that has a list of people's names in column B of a worksheet called Summary. It starts at B9 and can vary in length. Most names in this list correspond have a worksheet of the same name but some don't.

I would like to be able to loop through each name in the list, find if there is a worksheet with the same name and, if there is, copy and paste the value in A1 of that worksheet into the cell next to the name in column C.

Is this possible?

Thank you
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
VBA Code:
With Sheets("Summary").Range("B:B")
    With Range(.Cells(9, 1), .Cells(Rows.Count, 1).End(xlUp)).Offset(0, 1)
        .FormulaR1C1 = "=IFERROR(INDIRECT(""'"" & RC[-1]&""'!A1""),"""")"
        .Value = .Value
    End With
End With
 
Upvote 0
also returns list of missing sheets
VBA Code:
Sub LoopNames()
    Dim cel As Range, msg As String
    For Each cel In Sheets("Summary").Range("B9", Sheets("Summary").Range("B" & Rows.Count).End(xlUp))
        On Error Resume Next
        cel.Offset(, 1) = Sheets(cel.Text).Range("A1")
        If Err.Number <> 0 Then msg = msg & vbCr & cel
        On Error GoTo 0
    Next cel
    If msg <> "" Then MsgBox msg, , "Not found"
End Sub

code supplied by @mikerickson does not contain a loop and therefore is much faster
 
Last edited:
Upvote 0
Another way - bit less slick than the other Folk, but I'd already made the effort!!

VBA Code:
Sub check_name()
Dim sht As Worksheet
Dim lst_rw As Integer
Dim fnd_rw As Integer
Dim rng As Range
Dim fnd As Range

With Sheets("Summary")

    lst_rw = .Range("B500").End(xlUp).Row
     Set rng = .Range("B9:B" & lst_rw)
    
    For Each sht In Me.Sheets
        Set fnd = rng.Find(sht.Name, LookIn:=xlValues)
        If Not fnd Is Nothing Then .Range("C" & fnd.Row).Value = sht.Range("A1").Value
    
    Next

End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

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