For Next...is there a way to increase the loop

cmazur71

Board Regular
Joined
Aug 7, 2003
Messages
61
In my example, i start with "For i = 1 to a"
During the loop, i am inserting a row, so I want a to increase a by 1
Is there a way to make a increase everytime i insert a row?


a = Worksheets("Clean").Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To a
If Worksheets("Clean").Cells(i, 1).Value >= 6000000 And Worksheets("Clean").Cells(i, 1).Value <= 7999999 Then
Worksheets("Clean").Cells(i, 5).Clear 'clears the List Price
Worksheets("Clean").Cells(i, 7).Clear 'clears the Discount
Worksheets("Clean").Cells(i, 9).Clear 'clears the Purchase Price
Worksheets("Clean").Rows(i).Insert shift:=xlDown, CopyOrigin:=xlFormatFromAbove 'inserts a row above
i = i + 1 'next row
a = a + 1 'add a row to the total number of rows in Clean
End If
Next
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,735
Hi cmazur71,

If you are inserting or deleting one row at time like this it best to work bottom to top like so:

VBA Code:
Option Explicit
Sub Macro1()

    Const xlFormatFromAbove = 0

    Dim a As Long, i As Long
    
    Application.ScreenUpdating = False
    
    a = Worksheets("Clean").Cells(Rows.Count, 1).End(xlUp).Row

    For i = a To 1 Step -1
        If Worksheets("Clean").Cells(i, 1).Value >= 6000000 And Worksheets("Clean").Cells(i, 1).Value <= 7999999 Then
            Worksheets("Clean").Cells(i, 5).Clear 'clears the List Price
            Worksheets("Clean").Cells(i, 7).Clear 'clears the Discount
            Worksheets("Clean").Cells(i, 9).Clear 'clears the Purchase Price
            Worksheets("Clean").Rows(i).Insert shift:=xlDown, CopyOrigin:=xlFormatFromAbove 'inserts a row above
        End If
    Next i
    
    Application.ScreenUpdating = True
    
End Sub

Hope that helps,

Robert
 
Solution

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,273
Office Version
  1. 2010
Platform
  1. Windows
a
With Worksheets("Clean") - End With
might speed things up a bit more?
 

cmazur71

Board Regular
Joined
Aug 7, 2003
Messages
61
Hi cmazur71,

If you are inserting or deleting one row at time like this it best to work bottom to top like so:

VBA Code:
Option Explicit
Sub Macro1()

    Const xlFormatFromAbove = 0

    Dim a As Long, i As Long
   
    Application.ScreenUpdating = False
   
    a = Worksheets("Clean").Cells(Rows.Count, 1).End(xlUp).Row

    For i = a To 1 Step -1
        If Worksheets("Clean").Cells(i, 1).Value >= 6000000 And Worksheets("Clean").Cells(i, 1).Value <= 7999999 Then
            Worksheets("Clean").Cells(i, 5).Clear 'clears the List Price
            Worksheets("Clean").Cells(i, 7).Clear 'clears the Discount
            Worksheets("Clean").Cells(i, 9).Clear 'clears the Purchase Price
            Worksheets("Clean").Rows(i).Insert shift:=xlDown, CopyOrigin:=xlFormatFromAbove 'inserts a row above
        End If
    Next i
   
    Application.ScreenUpdating = True
   
End Sub

Hope that helps,

Robert
Thank you...it worked perfectly
 

Watch MrExcel Video

Forum statistics

Threads
1,127,643
Messages
5,626,059
Members
416,159
Latest member
CheeseBurger5

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