Row copy macro out of control

scoha

Active Member
Joined
Jun 15, 2005
Messages
428
I cannot figure out why my row copy macro goes out of control and copies the same row 100 times to the point it crashes. My code is :
Code:
Private Sub Worksheet_Activate()

Dim i As Long
Dim LR As Long
   
LR = Sheets("OpenIssues").Range("B" & Rows.Count).End(xlUp).Row

For i = 7 To LR

 If Sheets("OpenIssues").Range("D" & i) = "Closed" And Sheets("OpenIssues").Range("L" & i) > 0 Then
    Sheets("OpenIssues").Rows(i).Copy
    Sheets("ClosedIssues").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
 End If

Next i
    
For i = 7 To LR
    
 If Sheets("OpenIssues").Range("D" & i) = "Closed" And Sheets("OpenIssues").Range("L" & i) > 0 Then
    Sheets("OpenIssues").Rows(i).EntireRow.Delete
 End If

Next i
Exit Sub

It fails on the
Code:
Sheets("OpenIssues").Rows(i).Copy
line.

Any ideas?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
What is i set to to start with ? Try setting to 7 ( or whatever start number) manually before your loop. Also I wouldn't have another if statement inside a second loop of the same conditions. Use a else if inside the same loop.

Try hitting debug (f8) and going through the code. When it gets to that line hover over i to see it's value. This will also show you how the data and loop are working
 
Upvote 0
Omit the "s" in your line:

Sheets("OpenIssues").Rows(i).Copy

making it

Sheets("OpenIssues").Row(i).Copy
 
Upvote 0
Your code is fine, the only thing you need to make it work is to disable events while the code runs.

Code:
Private Sub Worksheet_Activate()
Application.EnableEvents = False
 
Dim i As Long
Dim LR As Long
 
LR = Sheets("OpenIssues").Range("B" & Rows.Count).End(xlUp).Row
 
For i = 7 To LR
 
 If Sheets("OpenIssues").Range("D" & i) = "Closed" And Sheets("OpenIssues").Range("L" & i) > 0 Then
    Sheets("OpenIssues").Rows(i).Copy
    Sheets("ClosedIssues").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
 End If
 
Next i
 
For i = 7 To LR
 
 If Sheets("OpenIssues").Range("D" & i) = "Closed" And Sheets("OpenIssues").Range("L" & i) > 0 Then
    Sheets("OpenIssues").Rows(i).EntireRow.Delete
 End If
 
Next i
Application.EnableEvents = True
End Sub

Although you could trim it down a little to something like

Code:
Private Sub Worksheet_Activate()
    Application.EnableEvents = False
Dim i As Long, LR As Long
With Sheets("OpenIssues")
    LR = .Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 7 Step -1
    If .Cells(i, "D") = "Closed" And .Cells(i, "L") > 0 Then
       .Rows(i).Copy
    Sheets("ClosedIssues").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
       .Rows(i).EntireRow.Delete
    End If
Next i
End With
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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