Copy range within nested for each loops

trazer985

Board Regular
Joined
Jan 4, 2011
Messages
134
Hi everyone,

I'm trying to get this work and I can't for the life of me figure out why it doesn't :(

I've got a few workbooks in a folder, and if any of them contain "ValueIWant" in range A4:T4 in any of their sheets, I want to copy the range of A6:T500 into a summary sheet and paste it there. I'm aware that I haven't done the lastrow stuff for the paste destination, feel free to add that too, but i can probably fix that, its erroring out on me when i put the for each ws loop in and I'm stumped as to why. Hopefully it's something simple!

Thanks in advance.



Code:
Sub CopySelectedRanges()
     
    Dim Path            As String
    Dim FileName        As String
    Dim Wkb             As Workbook
    Dim ws              As Worksheet
    
    Application.DisplayAlerts = False
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Path = "C:\Users\Me\Downloads\Folder" 'Change as needed
    FileName = Dir(Path & "\*.xl*", vbNormal)
    Do Until FileName = ""
        Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)
            For Each ws In Wkb.Worksheets
                 Set Rng = ws.Range("A4:T4")
                        For Each Cell In Rng
            If Cell.Value = "ValueIWant" Then
        'Sheets("Summary").Range("A1").Value = "Yes"
        Sheets("Sheet1").Range("A5:T500").Copy Destination:=Sheets("Summary").Range("A10")
            End If
            Next Cell
        Next ws
        
    'Next
       'Next ws
        'Wkb.Close False
        FileName = Dir()
    Loop
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
     
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try
Code:
ws.Range("A5:T500").Copy Destination:=thisworkbook.Sheets("Summary").Range("A10")
 
Upvote 0
Does it open a workbook?

Yes,
it opens each workbook, checks each sheet if there's a specific value in the range and if there is, copies a range from that sheet into the summary sheet of the master workbook where this macro is based. Sorry if that wasn't clear at the start.
 
Upvote 0
That's what it's meant to be doing, but you said that you are getting an error on this line
Code:
For Each ws In Wkb.Worksheets
in which case does it open the first workbook in the folder?
 
Upvote 0
That's what it's meant to be doing, but you said that you are getting an error on this line
Code:
For Each ws In Wkb.Worksheets
in which case does it open the first workbook in the folder?

Ahha! progress. When I closed the open workbooks this progressed, and then stops at " Sheets("Sheet1").Range("A5:T500").Copy Destination:=Sheets("Summary").Range("A10")" subscript out of range.
"
 
Upvote 0
Try what I suggested in post#2
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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