delete entire row with condition (simple but failed)

auzanoo

Board Regular
Joined
Jan 6, 2020
Messages
63
Office Version
  1. 2007
Platform
  1. Windows
dear expert,

i have a data and code, but it's failed.

from this:
GOPI09:29-12:00
TOPIK TERKINI10:26-10:27
TOPIK TERKINI11:45-11:47
BEPANNAH(R)12:00-14:00


should be this:
GOPI09:29-12:00
BEPANNAH(R)12:00-14:00

so, the time should continue from one to another one.

but, the result is like this:
GOPI09:29-12:00
TOPIK TERKINI11:45-11:47



this is my code,
VBA Code:
Sub DeleteRowswithSpecificValue()

Blank = Range("A1", Range("A1").End(xlDown)).Rows.Count

For i = 1 To Blank
If Right(Cells(i, 2),5) <> Left(Cells(i+1, 2),5) Then
Cells(i+1, 2).EntireRow.Delete

End If
Next i
End Sub

and i want to add exceptional:
if the time is not so far (around 1 or 2 minutes), then dont delete the row.

for example,
from this:
GOPI09:29-12:00
TOPIK TERKINI10:26-10:27
TOPIK TERKINI11:45-11:47
BEPANNAH(R)12:02-14:00


to this:
GOPI09:29-12:00
BEPANNAH(R)12:02-14:00


i dont know what to do with this one.

thanks in advance!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Looks like you want to compare the rest of the line with row 1 only. So, row 1 is a fix reference. Then you'll get the result you're looking for.

Ops.... don't really understand on the time difference.
 
Upvote 0
You should loop from bottom to top when deleting as the rows shift up, try

VBA Code:
For i = Blank to 1

Rich (BB code):
For i = Blank to 1 Step -1
would be better ... ;)
 
Upvote 0
I guess you want something like this?
VBA Code:
Sub DeleteRowswithSpecificValue()

Dim Blank As Long, T As Long

Blank = Range("A1", Range("A1").End(xlDown)).Rows.Count
For i = 1 To Blank
    If Not IsEmpty(Cells(i + 1, 2)) Then
        T = Abs(DateDiff("n", Right(Cells(i, 2), 5), Left(Cells(i + 1, 2), 5)))
        If T > 2 Then
            Cells(i + 1, 2).EntireRow.Delete
            i = i - 1
        End If
    End If
Next i

End Sub
 
Upvote 0
I guess you want something like this?
VBA Code:
Sub DeleteRowswithSpecificValue()

Dim Blank As Long, T As Long

Blank = Range("A1", Range("A1").End(xlDown)).Rows.Count
For i = 1 To Blank
    If Not IsEmpty(Cells(i + 1, 2)) Then
        T = Abs(DateDiff("n", Right(Cells(i, 2), 5), Left(Cells(i + 1, 2), 5)))
        If T > 2 Then
            Cells(i + 1, 2).EntireRow.Delete
            i = i - 1
        End If
    End If
Next i

End Sub

Yes! this is perfectly works! thank you so much.

but i got a problem...
my time format is 02.00-25.59 :(
and the macro doesnt work at 24.00-25.59.
could you handle this? thank you:)
 
Upvote 0
Yes! this is perfectly works! thank you so much.

but i got a problem...
my time format is 02.00-25.59 :(
and the macro doesnt work at 24.00-25.59.
could you handle this? thank you:)
You can use Replace function to replace dot to double dot in before doing calculation.
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,593
Members
452,654
Latest member
mememe101

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