For Each Loop Premature Termination

StarvingDog

New Member
Joined
May 28, 2019
Messages
8
VBA novice and first time poster here :cool:. I'm attempting to copy specific rows from two different sheets ("Chemistry OOS Log" and "Microbiology OOS Log") to a third sheet at which point it will be filtered based on the value in column "L". All sheets are in the same workbook. The initial ranges should only be copied if the cell value in column "O" is nothing. My code is "almost" working... It is only copying/pasting the last specified row from each sheet. I haven't started on the code to filter the "Open OOS" sheet... I'll work on that after this issue is resolved. Thank you in advance!


Code:
Sub Transfer_OOS()


    Worksheets("Open OOS").Range("A2:Q100").Clear


    ActiveWorkbook.Sheets("Chemistry OOS Log").Activate
    Call LoopAndCopy
    ActiveWorkbook.Sheets("Microbiology OOS Log").Activate
    Call LoopAndCopy


End Sub




Sub LoopAndCopy()


    Application.ScreenUpdating = False
    
    Dim c As Range
    Dim Last_Row As Long
    
    Last_Row = Sheets("Open OOS").Range("A65536").End(xlUp).Row
    
        For Each c In Range(("O2:O") & Cells(Rows.Count, "O").End(xlUp).Row)
            If Not IsEmpty(c.Value) Then
            Else
                c.EntireRow.Copy
                Sheets("Open OOS").Activate
                Cells(Last_Row + 1, 1).PasteSpecial xlValues
                Cells(Last_Row + 1, 1).PasteSpecial xlFormats
            End If
        Next c
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True


End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Rich (BB code):
Sub LoopAndCopy()

      Last_Row = Sheets("Open OOS").Range("A65536").End(xlUp).Row
    
        For Each c In Range(("O2:O") & Cells(Rows.Count, "O").End(xlUp).Row)
            If Not IsEmpty(c.Value) Then
            Else
                c.EntireRow.Copy
                Sheets("Open OOS").Activate
                Cells(Last_Row + 1, 1).PasteSpecial xlValues
                Cells(Last_Row + 1, 1).PasteSpecial xlFormats
            End If
            Last_Row = Last_Row + 1
        Next c
    
End Sub
You do not increment Last_Row. So, everything goes to the same row.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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