excel - Delete row if 2 conditions are met - change in time and same value in another column -
i trying code loop in excel vba following:
i have column long date + time. have column has text string.
i want delete entire row if change in time (row - row above) less 0:00:05 , value of string same (row vs row above)
i'm running problems if condition, 5 second portion, doesn't it...
for lrow = lastrow firstrow step -1 'we check values in d column .cells(lrow, "d") if not iserror(.value) if (cells(i,"d") - cells(i-1,"d")) > (0:00:05) , (cells.value(i,"f") = cells.value(i-1,"f") .entirerow.delete end if end next lrow
ok, code above sucks. going explore different tack. want compare column f - current cell 1 below it. if same, trigger next clause - seeing if current cell in d < 0:00:05 seconds different 1 below it, if so, delete (or store information delete outside loop).
if 2 cells in f not same, skip next cell.
that make more sense? let me work code , post here.
your time need represented number dates in excel calculated number of days since given date (e.g. today 41458 days 1st jan 1900). divide day 24 1 hour , 12 5 mins example:
edited
i have re-written code following comments below changing other factors time comparison.
the code below example of how make macro automatically delete new rows added. code should added sheet making changes on (rather module or workbook). if not want take automatic approach either add option check user first or change sub , pass in range object representing cell on row has been added (let me know if need this):
private sub worksheet_change(byval target range) dim newstr dim newdate dim prevstr dim prevdate dim interval newstr = range("d" & target.row).value newdate = range("f" & target.row).value prevstr = range("d" & target.row - 1).value prevdate = range("f" & target.row - 1).value interval = (1 / 24) / 12 ' 5 mins if newstr = prevstr , abs(newdate - prevdate) <= interval target.entirerow.delete end if end sub
i have set test data , above seems work nicely on sample data have set up, let me know if have questions.
Comments
Post a Comment