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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
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
 

Forum statistics

Threads
1,141,756
Messages
5,708,333
Members
421,566
Latest member
7Nabisco

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
Top