Loop Through Multiple Workbooks and Worksheets Using An Array
I have the following code that will look for all the workbooks and search all the worksheets for a specific value and copy that value into the workbook that runs the macro. The code will open each workbook and cycle through each worksheet with the exception of the first worksheet and copy the value identified in the array and paste the results into the workbook running the macro. My question is, is there a way to copy the value from the workbooks without opening each workbook and cycle through each worksheet and paste? Looking for a way to simplify the code. As always, any help is greatly appreciated.
Code:
Public Sub Block()
Dim wbk As Workbook
Dim Filename As String
Dim Path As String
Dim FirstAddress As String
Dim MyArr As Variant
Dim Rng As Range
Dim Rcount As Long
Dim i As Long
Dim NewSh As Worksheet
Dim sh As Worksheet
Dim LastRow1 As Long
Dim x As Range
Dim Tgt As Range
Sheets("BlockList").Cells.Clear
' With ActiveSheet
' LastRow1 = .Cells(.Rows.Count, "A").End(xlUp).Row
' End With
'
' Set Tgt = ThisWorkbook.Sheets("DL").Cells(Rows.Count, 1).End(xlUp)(2)
Path = "C:\LindaReports\"
Filename = Dir(Path & "*.xls*")
'--------------------------------------------
'OPEN EXCEL FILES
Do While Len(Filename) > 0 'IF NEXT FILE EXISTS THEN
Set Tgt = ThisWorkbook.Sheets("BlockList").Cells(Rows.Count, 1).End(xlUp)(2)
Set wbk = Workbooks.Open(Path & Filename)
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Fill in the search Value
MyArr = Array("QC*")
'Add new worksheet to your workbook to copy to
'You can also use a existing sheet like this
' Set NewSh = Sheets("DL")
' Set NewSh = Worksheets.Add
' NewSh.Name = "DL"
' Cells(1, 1) = "EF"
For Each sh In ActiveWorkbook.Worksheets
Select Case "Summary"
Case Else
With sh.Cells.Range("A1:Z100")
' LastRow1 = Sheets("DL").Cells(sh.Rows.Count, "A").End(xlUp).Row
' .Range ("A1:Z100")
'Range("A1", Columns("A").SpecialCells(xlCellTypeLastCell)).Delete
' Cells(LastRow1 + 1, 1).Activate
Rcount = 0 + Rcount
For i = LBound(MyArr) To UBound(MyArr)
Set Rng = .Find(What:=MyArr(i), _
after:=.Cells(.Cells.Count), _
LookIn:=xlFormulas, _
Lookat:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
FirstAddress = Rng.Address
Do
Rcount = Rcount + 1
' Rng.Copy NewSh.Range("A" & Rcount)
' NewSh.Range("B" & Rcount).Resize(Rng.Rows.Count).Value = sh.Name
' NewSh.Range(LastRow1).Activate
' Use this if you only want to copy the value
Tgt.Range("A" & Rcount).Value = wbk.Name
Tgt.Range("B" & Rcount).Resize(Rng.Rows.Count).Value = sh.Name
Tgt.Range("C" & Rcount).Value = Rng.Value
' NewSh.Cells(LastRow1 + 1, Rcount) = Rng.Value
Set Rng = .FindNext(Rng)
Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
End If
Next i
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Select
Next sh
' Range("A1").CurrentRegion.Copy Tgt
' MsgBox Filename & " has opened"
wbk.Close True
Filename = Dir
Rcount = 0
Loop
End Sub