FOR IF continual loop error

Enzo_Matrix

Board Regular
Joined
Jan 9, 2018
Messages
113
I am trying to get my table to insert a blank row after a set date, but this code is creating a continual loop that I need to break manually.

I'm not sure what I have done wrong, but can someone please help me correct this code or suggest something that would work better?
Code:
Sub InsrtRow()

Dim C As Range
    
    With Sheets("Weld")
       For Each C In .Range("G5", .Cells(Rows.Count, 7).End(xlUp))
            If C >= Date + 2 Then
                C.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
            End If
        Next
    End With


End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Write a for/next loop from the last row to the first instead, otherwise you have to adjust the counter each time to prevent repetition.
 
Upvote 0
When deleting/inserting rows you should always work bottom up, rather than top down.
try
Code:
Sub InsrtRow()

Dim i As Long
    
    With Sheets("Weld")
       For i = .Range("G" & Rows.count).End(xlUp).Row To 5 Step -1
            If Range("G" & i).Value >= Date + 2 Then
                Rows(i).EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
            End If
        Next i
    End With


End Sub
 
Upvote 0
This works very well thank you,

it does however add a row for every date after the '>=Date +2'. I'm wondering if this can be done once instead of for each instance of the date range.
April 12 April 12
April 13 April 13

April 14 April 14
April 15
April 15 April 16

April 16...

I was also asked if this can be modified to only work for week days. Is that possible?
 
Upvote 0
Is this what you mean
Code:
Sub InsrtRow()

Dim cl As Range
    
    With Sheets("pcode")
       For Each cl In .Range("G5", .Range("G" & Rows.count).End(xlUp))
            If cl.Value >= Application.WorkDay(Date, 2) Then
                cl.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
                Exit For
            End If
        Next cl
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,068
Messages
6,128,592
Members
449,460
Latest member
jgharbawi

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