VBA Loop Copy and Paste, not looping through all worksheets

csimonds

Board Regular
Joined
Oct 2, 2011
Messages
73
Hello,

I have the below code, I was hoping it would look at all sheets in workbook, if N4=copy, then take B9-22 and G9-22 and paste in row 24. Then move to the next sheet with N4=copy and do the same. It works for one worksheet, but does not loop through the workbook. If anyone could please provide some advice as to why this code is not working it would be greatly appreciated.

Thank you

Code:
Sub CopyData()    Dim wks         As Worksheet


    With ActiveWorkbook
        For Each wks In ActiveWorkbook.Worksheets
            If LCase(wks.Range("N4").Value) = "copy" Then
                Range("B9:B22").Select
                Selection.Copy
                Range("B24").Select
                ActiveSheet.Paste
                
                Range("G9:G22").Select
                Selection.Copy
                Range("G24").Select
                ActiveSheet.Paste

                Range("A1").Select

            End If
        Next wks
    End With
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Activate the sheet on each loop
You can make this work by adding one line of code inside your For Each Loop
wks.Activate

Sub CopyData()
Dim wks As Worksheet
With ActiveWorkbook
For Each wks In ActiveWorkbook.Worksheets
wks.Activate
If LCase(wks.Range("N4").Value) = "copy" Then
Range("B9:B22").Select
Selection.Copy
Range("B24").Select
ActiveSheet.Paste

Range("G9:G22").Select
Selection.Copy
Range("G24").Select
ActiveSheet.Paste


Range("A1").Select


End If
Next wks
End With
End Sub
 
Upvote 0
No need for selecting cells and sheets.

Code:
Sub CopyData()
Dim wks As Worksheet


    For Each wks In ActiveWorkbook.Worksheets
        
        With wks
            
            If LCase(.Range("N4").Value) = "copy" Then
                .Range("B9:B22").Copy .Range("B24")
                .Range("G9:G22").Copy .Range("G24")
            End If
        
        End With
    
    Next wks
    
End Sub
 
Last edited:
Upvote 0
Thank you both for your replies. This is now working perfectly for my needs.

Thanks

No need for selecting cells and sheets.

Code:
Sub CopyData()
Dim wks As Worksheet


    For Each wks In ActiveWorkbook.Worksheets
        
        With wks
            
            If LCase(.Range("N4").Value) = "copy" Then
                .Range("B9:B22").Copy .Range("B24")
                .Range("G9:G22").Copy .Range("G24")
            End If
        
        End With
    
    Next wks
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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