For each... in doesn't loop properly

chi90

New Member
Joined
Jul 21, 2014
Messages
6
Hi everyone,

this is my first post so I apologize in advance for any mistakes I can make posting.

Here's my problem: I wrote a macro to loop through a range of cells and then, if the cells' value is more than 0, to copy the cell and the adjacent ones to another sheet.
The problem is that it works only for the last positive cell in the range. So, it checks all the cells but only copy the last set of cells.
I hope I've made it clear :)
I searched the forum but couldn't find anything to help me.
Thanks in advance!

Code:
Sub ARCHIVE2()
    
   Application.ScreenUpdating = False
 
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    Dim r1 As Range
    Dim r2 As Range


    Set wb1 = ActiveWorkbook
    Set wb2 = Workbooks("Archive")


    Set sh1 = wb1.ActiveSheet
    Set sh2 = wb2.Worksheets("Sheet1")
    
lMaxRows = sh2.Cells(Rows.Count, "E").End(xlUp).Row


    Set r1 = sh1.Range("o4:o54")


    Set r2 = sh2.Range("e" & lMaxRows + 1)
        Dim c As Range
    
    For Each c In r1
    On Error Resume Next
'test if cell is empty
        If c.Value > "0" Then
            'copy adjacent cells
            c.Resize(, 7).Copy
            r2.PasteSpecial Paste:=xlValues
             
        End If
        Next c
        
 Application.ScreenUpdating = True


End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,215,960
Messages
6,127,942
Members
449,411
Latest member
sdescharme

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