stop code running through all rows

Mark_G

Board Regular
Joined
Aug 6, 2004
Messages
123
I have created the following section of code. WHat I am trying to do is just to have it run through the first 8784 rows, not the entire worksheet. I have the "for" statement at the top, but it continues to run through all the rows, slowing the macro down considerably. How do I get it to run just on the rows I want it to. Column A is always filled to row 8784 and I tried to use the "last row" but that did not work either, even though in break mode I could see that it was picking up the last row.

Thanks for any help on this one.

Code:
Sub data_correction()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

For Each cell In Sheets("2005").Range("I4:I8784")
  
If cell.Offset(-2, -8).Value <> "" And cell.Offset(-2, -8).Value = cell.Offset(-2, -2).Value And cell.Offset(-2, -7).Value <> cell.Offset(-2, 0).Value Then
    Range(cell.Offset(-2, -6), cell.Offset(-2, 19)).Insert
        cell.Offset(-3, 0).Value = cell.Offset(-3, -7).Value
        cell.Offset(-3, -2).Value = cell.Offset(-3, -8).Value
        Range(cell.Offset(-3, 1), cell.Offset(-3, 19)).Value = "999"
    End If
    
If cell.Offset(-2, -8).Value <> "" And cell.Offset(-2, -8).Value <> cell.Offset(-2, -2).Value Then
    Range(cell.Offset(-2, -6), cell.Offset(-2, 19)).Insert
        cell.Offset(-3, -2).Value = cell.Offset(-3, -8).Value
        cell.Offset(-3, 0).Value = cell.Offset(-3, -7).Value
        Range(cell.Offset(-3, 1), cell.Offset(-3, 19)).Value = "999"
    End If

Next cell

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

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
Hi Mark

I have not examined your code thoroughly but I see that you have Insert instructions inside the loop. The Inserts will extend the original range.

For example, this simple code never ends (until it reaches the end of the worksheet). You can check what happens to the original range.

Code:
Sub LoopInsert()
Dim cR As Range, rR As Range

Set rR = Range("A1:A10")

For Each cR In Range("A1:A10")
    cR.Insert
    MsgBox cR.Address & ", " & rR.Address
Next
End Sub


So, I think that in this case you should refer to the loop cell using the row number

Instead of

Code:
For Each cell In Sheets("2005").Range("I4:I8784")

Something like

Code:
With  Sheets("2005")
    For lRow=4 to 8784


   Cell  --> .Range("I" & lRow)

Hope this helps you finding the solution
PGC
 
Upvote 0

Forum statistics

Threads
1,215,042
Messages
6,122,810
Members
449,095
Latest member
m_smith_solihull

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