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

cmazur71

Board Regular
Joined
Aug 7, 2003
Messages
63
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
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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
 
Upvote 0
Solution
a
With Worksheets("Clean") - End With
might speed things up a bit more?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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