Loop Through Sheets Not Working

pliskers

Active Member
Joined
Sep 26, 2002
Messages
461
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to write code that will loop through all the sheets in a workbook and, on each sheet, copy the contents of cells A1:B1 on that sheet to from row 4 through the last row on that sheet (as defined by column C). The macro does not move through the sheets. Rather, it's performing the copy paste from each sheet but staying on the first tab. I must be missing a step. Can someone please have a look and let me know?

Thank you!

Sub Loop_()

Dim wb As ThisWorkbook
Dim ws As Worksheet
Dim TableName As String
Dim LstObj As ListObjects

For Each ws In ThisWorkbook.Worksheets
With ws

lastrow = Range("C1000000").End(xlUp).Row
ws.Range("A1:B1").Copy
Range("A4:B" & lastrow).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
lastrow = vbNullString


End With
Next ws

End Sub
 
What do you mean by the "last version of the code"?
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The one you posted "Try it like" 49 minutes ago - 5 comments up. Just need the copy/paste of the contents of A1:B1 (which are formulas) before the paste values step.
 
Upvote 0
Ok, try
VBA Code:
Sub Loop_()

    Dim wb As ThisWorkbook
    Dim ws As Worksheet
    Dim TableName As String
    Dim LstObj As ListObjects

For Each ws In ThisWorkbook.Worksheets
       
        lastRow = ws.Range("C1000000").End(xlUp).Row
         ws.Range("A1:B1").Copy ws.Range("A4:B" & lastRow)
         ws.Range("A4:B" & lastRow).Value = ws.Range("A4:B" & lastRow).Value
         
Next ws

End Sub
 
Upvote 0
Ok, try
VBA Code:
Sub Loop_()

    Dim wb As ThisWorkbook
    Dim ws As Worksheet
    Dim TableName As String
    Dim LstObj As ListObjects

For Each ws In ThisWorkbook.Worksheets
      
        lastRow = ws.Range("C1000000").End(xlUp).Row
         ws.Range("A1:B1").Copy ws.Range("A4:B" & lastRow)
         ws.Range("A4:B" & lastRow).Value = ws.Range("A4:B" & lastRow).Value
        
Next ws

End Sub
Perfect! I'm so grateful for your help!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,973
Members
449,059
Latest member
oculus

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