For Each not looping through worksheets

jtotman

New Member
Joined
Jul 6, 2018
Messages
9
Hi!

I am trying to consolidate data from multiple workbooks into one sheet in workbook. For some reason, I cannot get excel to loop through those every sheet the workbook and copy the data. I am including my code below. Any suggestions would be appreciated!

Code:
Sub BOCIndex()


Workbooks.Open Filename:=ThisWorkbook.Path & "\BOC Cust.xlsx"


Dim LastRowRange As Long
Dim LastRowIndex As Long
Dim ws As Worksheet


For Each ws In ActiveWorkbook.Worksheets


LastRowRange = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
                    
LastRowIndex = Workbooks("Deposits Consolidation.xlsm").Worksheets("Index").Range("B1").End(xlDown).Row + 1






Range("A4", "D" & LastRowRange).Copy
Workbooks("Deposits Consolidation.xlsm").Worksheets("Index").Range("B" & LastRowIndex).PasteSpecial xlPasteValues


Next ws


'Workbooks("BOC Cust.xlsx").Close savechanges:=False


End Sub
 
Last edited by a moderator:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi. Try:

Code:
Sub BOCIndex()
 Dim LastRowRange As Long
 Dim LastRowIndex As Long
 Dim ws As Worksheet
  Workbooks.Open Filename:=ThisWorkbook.Path & "\BOC Cust.xlsx"
   For Each ws In ActiveWorkbook.Worksheets
    With ws
     LastRowRange = .Cells.Find(What:="*", _
     After:=.Range("A1"), _
     LookAt:=xlPart, _
     LookIn:=xlFormulas, _
     SearchOrder:=xlByRows, _
     SearchDirection:=xlPrevious, _
     MatchCase:=False).Row
     
     LastRowIndex = Workbooks("Deposits Consolidation.xlsm").Worksheets("Index").Range("B1").End(xlDown).Row + 1

      .Range("A4", "D" & LastRowRange).Copy
      Workbooks("Deposits Consolidation.xlsm").Worksheets("Index").Range("B" & LastRowIndex).PasteSpecial xlPasteValues
    End With
   Next ws
'Workbooks("BOC Cust.xlsx").Close savechanges:=False
End Sub
 
Last edited:
Upvote 0
Hi. Thank you both for your suggestions, but neither one seems to correct the problem. They both run without errors, but they only copy the first sheet of data while ignoring the subsequent sheets.
 
Upvote 0
Assuming the code is in the Deposits Consolidation workbook, try this:

Code:
Sub BOCIndex()
 Dim LastRowRange As Long
 Dim LastRowIndex As Long
 Dim wb as workbook
 Dim ws As Worksheet
 Dim wsIndex as Worksheet
set wsIndex = Thisworkbook.Worksheets("Index")

  Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "\BOC Cust.xlsx")
   For Each ws In wb.Worksheets
    With ws
     LastRowRange = .Cells.Find(What:="*", _
     After:=.Range("A1"), _
     LookAt:=xlPart, _
     LookIn:=xlFormulas, _
     SearchOrder:=xlByRows, _
     SearchDirection:=xlPrevious, _
     MatchCase:=False).Row
    
     LastRowIndex = wsIndex.Cells(wsIndex.Rows.Count, "B").End(xlUp).Row + 1

      .Range("A4", "D" & LastRowRange).Copy
      wsindex.Range("B" & LastRowIndex).PasteSpecial xlPasteValues
    End With
   Next ws
'wb.Close savechanges:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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