excel - if single cell value is found in a range then delete entire row -
i working daily data recorded 31 days of data per multiple stations , need vba code remove leap year days. have list of dates of data recorded , list of years not leap years want delete. delete 30 , 31 days, used basic following code:
dim lastrow, long
lastrow = activesheet.cells(65536, 1).end(xlup).row
for = 1 lastrow 'delete 31st days february if activesheet.range("d" & i) = 2 , activesheet.range("e" & i) = 31 rows(i).select selection.delete shift:=xlup end if next pretty simple , works nicely hoping able similar find non-existent date (ie. 02/29/non-leapyear) within data , delete row has turned out extremely difficult match value within range. thinking along lines of this:
dim lastrow, long, leapyear workbook
set leapyear = workbooks("leapyears.xlsx")
lastrow = activesheet.cells(65536, 1).end(xlup).row
for = 1 lastrow 'obviously have problem trying match cell range if activesheet.range("d" & i) = leapyear.sheets(1)range("c2:c90") rows(i).select selection.delete shift:=xlup end if next any or way handle appreciated!
modified version of answer this question
sub sample() dim startingscreenupdatevalue boolean dim startingeventsvalue boolean dim startingcalculations xlcalculation application startingscreenupdatevalue = .screenupdating startingeventsvalue = .enableevents startingcalculations = .calculation .screenupdating = false .enableevents = false .calculation = xlcalculationmanual end dim vartestvalues variant vartestvalues = workbooks("leapyears.xlsx").sheets(1).range("c2:c90") rows(1).insert [a1].formular1c1 = "tempheader1" [a1].autofill destination:=range("a1:h1"), type:=xlfilldefault range("d1").autofilter field:=4, criteria1:=application.transpose(vartestvalues), operator:=xlfiltervalues range("d2", range("d" & rows.count).end(xlup)) _ .specialcells(xlcelltypevisible).entirerow.delete activesheet.autofiltermode = false rows(1).delete application .screenupdating = startingscreenupdatevalue .enableevents = startingeventsvalue .calculation = startingcalculations end end sub note: code runs assuming data has headers if not please advise.
remember run code on copy of data , not actual data until confident working 100%.
Comments
Post a Comment