For Each Worksheet Loop Not Moving To Next Worksheet

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
This chunk is not moving from ws to ws like you would expect. I don't know why. Any ideas?

Code:
For Each ws In wkbk_LI.Worksheets

    c = ActiveSheet.Name


    wkbk_LI.Worksheets(c).Cells.ClearContents


    'Dependents
    Set rng = wkbk_D.Worksheets(c).Range("A6")
    Set rng = Range(rng, rng.End(xlDown))
    rng.Copy
    wkbk_LI.Worksheets(c).Range("A1").PasteSpecial Paste:=xlPasteValues


    Set rng = wkbk_D.Worksheets(c).Range("M6")
    Set rng = Range(rng, rng.End(xlDown))
    rng.Copy
    wkbk_LI.Worksheets(c).Range("B1").PasteSpecial Paste:=xlPasteValues


    'Independents
    Set rng = wkbk_I.Worksheets(c).Range("FN6")
    Set rng = Range(rng, rng.End(xlDown))
    rng.Copy
    wkbk_LI.Worksheets(c).Range("D1").PasteSpecial Paste:=xlPasteValues


    Set rng = wkbk_I.Worksheets(c).Range("FO6")
    Set rng = Range(rng, rng.End(xlDown))
    rng.Copy
    wkbk_LI.Worksheets(c).Range("E1").PasteSpecial Paste:=xlPasteValues


    Set rng = wkbk_I.Worksheets(c).Range("CZ6")
    Set rng = Range(rng, rng.End(xlDown))
    rng.Copy
    wkbk_LI.Worksheets(c).Range("F1").PasteSpecial Paste:=xlPasteValues


    Set rng = wkbk_I.Worksheets(c).Range("DA6")
    Set rng = Range(rng, rng.End(xlDown))
    rng.Copy
    wkbk_LI.Worksheets(c).Range("G1").PasteSpecial Paste:=xlPasteValues


    Set rng = wkbk_I.Worksheets(c).Range("AF6")
    Set rng = Range(rng, rng.End(xlDown))
    rng.Copy
    wkbk_LI.Worksheets(c).Range("H1").PasteSpecial Paste:=xlPasteValues


    Set rng = wkbk_I.Worksheets(c).Range("AJ6")
    Set rng = Range(rng, rng.End(xlDown))
    rng.Copy
    wkbk_LI.Worksheets(c).Range("I1").PasteSpecial Paste:=xlPasteValues


Next ws
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,664
Office Version
  1. 365
Platform
  1. Windows
Try
Code:
For Each Ws In wkbk_LI.Worksheets

 C = ActiveSheet.Name

    Ws.Cells.ClearContents


    'Dependents
    Set Rng = wkbk_D.Worksheets(C).Range("A6")
    Set Rng = Range(Rng, Rng.End(xlDown))
    Rng.Copy
    Ws.Range("A1").PasteSpecial Paste:=xlPasteValues


    Set Rng = wkbk_D.Worksheets(C).Range("M6")
    Set Rng = Range(Rng, Rng.End(xlDown))
    Rng.Copy
    Ws.Range("B1").PasteSpecial Paste:=xlPasteValues


    'Independents
    Set Rng = wkbk_I.Worksheets(C).Range("FN6")
    Set Rng = Range(Rng, Rng.End(xlDown))
    Rng.Copy
    Ws.Range("D1").PasteSpecial Paste:=xlPasteValues


    Set Rng = wkbk_I.Worksheets(C).Range("FO6")
    Set Rng = Range(Rng, Rng.End(xlDown))
    Rng.Copy
    Ws.Range("E1").PasteSpecial Paste:=xlPasteValues


    Set Rng = wkbk_I.Worksheets(C).Range("CZ6")
    Set Rng = Range(Rng, Rng.End(xlDown))
    Rng.Copy
    Ws.Range("F1").PasteSpecial Paste:=xlPasteValues


    Set Rng = wkbk_I.Worksheets(C).Range("DA6")
    Set Rng = Range(Rng, Rng.End(xlDown))
    Rng.Copy
    Ws.Range("G1").PasteSpecial Paste:=xlPasteValues


    Set Rng = wkbk_I.Worksheets(C).Range("AF6")
    Set Rng = Range(Rng, Rng.End(xlDown))
    Rng.Copy
    Ws.Range("H1").PasteSpecial Paste:=xlPasteValues


    Set Rng = wkbk_I.Worksheets(C).Range("AJ6")
    Set Rng = Range(Rng, Rng.End(xlDown))
    Rng.Copy
    Ws.Range("I1").PasteSpecial Paste:=xlPasteValues


Next Ws
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
60,057
Office Version
  1. 365
Platform
  1. Windows
Looping through the collection of worksheets does not select or activate them.

You either need to preface your ranges with the ws variable, i.e.
Code:
Set rng = ws.Range("A1:A10")
or Select/Activate the sheet as the first step in the loop, i.e.
Code:
ws.Activate
 

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
I was trying to step through it with F8 with the first sheet in the set to loop through partly visible. After I watched the "c" variable i saw that it was in fact stepping through each sheet, but not making the progress visible. I suppose the moral of the story is to add a watch and know that watching the screen update is not fool proof.
 

Forum statistics

Threads
1,147,632
Messages
5,742,228
Members
423,714
Latest member
ftp2jz

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
Top