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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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