Results 1 to 5 of 5

Thread: Delete rows based on a condition

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Delete rows based on a condition

    Hi,
    I have a sprea sheet with data related to irrigation of farms . First column contains the first day which the farm irrigated. The second column contains farm name and the 3rd column is duration of irrigation in days. I want to remove the irrigation which is in unusual time for each farm( in my case if (difference in date - irrigation duration) > 10 days (in other words if the farm have not received water for more than 10 days then remove the row related to that date.
    I have written the following code, but it does not work

    Sub pak3()
    
    Dim i As Integer, Lastrow As Long
    
    
    Sheet4.Activate
    Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 2 To Lastrow
        If Cells(i, 2).Value = Cells(i + 1, 2).Value Then
            If Cells(i + 1, 1).Value - Cells(i, 1).Value - Cells(i, 3).Value > 10 Then
                 If Cells(i + 2, 1).Value - Cells(i + 1, 1).Value - Cells(i + 1, 3).Value > 10 Then
                    Rows.Delete (i + 1)
                    Else:
                    Rows.Delete (i)
                End If
            End If
        End If
    Next i
            
            
    End Sub
    To make it clear I have attached my file. In this example rows 3 and 25(yellow rows) must be removed by code
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •