excel - how to find next date if it didn't find particular date using VBA -
i need here. have sheet 1 , sheet2. , in sheet1/2 have dates in column b , both sheet dates not same when commend select date print want vba select nearest date if coudn't find date. example:- if ask vba print date 12-aug-17 can selected in sheet1 in sheet 2 there no 12th aug has select 13th or 11th , print. in coding, if it’s in same date print both sheet. if fails show error.
please see below code.
sub nextdate() dim sh worksheet set sh2 = sheets("sheet2") set sh3 = sheets("sheet3") dim i, j2, j3, sh2endcell, sh3endcell integer dim w1enddate date w1enddate = application.inputbox("enter end date") ddate = range("b" & rows.count).end(xlup).row sh2endcell = sh2.range("b" & rows.count).end(xlup).row sh3endcell = sh3.range("b" & rows.count).end(xlup).row = 2 sh2endcell if sh2.range("b" & i).value = w1enddate j2 = exit end if
not sure if got problem description right, try this:
... ... sh2endcell = sh2.range("b" & rows.count).end(xlup).row dim mindatediff long dim closestdate mindatediff = 1000000 = 2 sh2endcell if abs(datediff("d", sh2.range("b" & i).value, w1enddate)) < mindatediff mindatediff = abs(datediff("d", sh2.range("b" & i).value, w1enddate)) closestdate = sh2.range("b" & i).value j2 = end if next
Comments
Post a Comment