Sheet loop not working

Lizard07

Board Regular
Joined
Jul 20, 2011
Messages
103
Hi - I have a code that I would like to loop through each sheet (except the sheets named "Activity Summary" and "Control Panel"), copy column H, then copy the contents of cell B7 into column G.

' Loop through all worksheets and copy driver name into column G
'For Each sh In ActiveWorkbook.Worksheets
' If sh.Name <> "Control Panel" And sh.Name <> "Activity Summary" Then
' Columns("H:H").Select
' Selection.Copy
'Columns("G:G").Select
'Range("G7").Activate
'ActiveSheet.Paste
'Range("B7").Select
' Selection.Copy
'Range("G15").Select
'Range(Selection, Selection.End(xlDown)).Select
'ActiveSheet.Paste
'Next

Any ideas?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Something like this will be much closer:

Code:
Sub loops()
'Loop through all worksheets and copy driver name into column G
    For Each sh In ActiveWorkbook.Worksheets
        If sh.Name <> "Control Panel" And sh.Name <> "Activity Summary" Then
            sh.Columns("H").Copy sh.Range("G7")
            sh.Range("B7").Copy sh.Range("G15", sh.Range("G15").End(xlDown))
        End If
    Next
End Sub

Inform yourself about the use of the variable called sh which qualifies the worksheet you are working on - within the loop.

PS: please use
Code:
 tags when you paste code on the forum.
 
Upvote 0
Yah I'm aware of that, but when I run the Macro (without the comment apostrophes) I receive the error message "Next without for"
 
Upvote 0
Yah I'm aware of that, but when I run the Macro (without the comment apostrophes) I receive the error message "Next without for"

You are missing an "End If" line.

Did you experiment with my suggestion?
 
Upvote 0
Yes I did and I received this error message "Application-defined or object-defined error"

Sub loops()
'Loop through all worksheets and copy driver name into column G
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> "Control Panel" And sh.Name <> "Activity Summary" Then
sh.Columns("H").Copy sh.Range("G")
sh.Range("B7").Copy sh.Range("G15", sh.Range("G15").End(xlDown))
End If
Next
End Sub

The only thing I've changed is the bolded code above, so the copy and paste ranges would match in size.
 
Upvote 0
Use code tags when posting codes as per forum rules

Try
Code:
Sub loops()
'Loop through all worksheets and copy driver name into column G
    Dim sh As Worksheet
    For Each sh In ActiveWorkbook.Worksheets
        If sh.Name <> "Control Panel" And sh.Name <> "Activity Summary" Then
            sh.Columns("H").Copy sh.Columns("G")
            sh.Range("B7").Copy sh.Range("G15", sh.Range("G15").End(xlDown))
        End If
    Next
End sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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