vba that search for a value if it matches find closest date -
i wrote code workbook have 2 worksheet´s, in proximopedido (sheet) in column "a" there range of values (integer number) , in colunm "b" there date associated, , in chekinglist (sheet) there colunm "a" values (wich must match "a" of proximopedido) , colunm "e" dates. if value of cell of checkinglist matches value of "a" of proximopedido search in "e" of chekinglist next ( or closest higher) date of "b" proximopedido.
sheet: checkinglist
a-----------------------------------------e
1----------------------------------2009-10-30 12:00
3 ---------------------------------2009-10-29 13:00
2---------------------------------2009-10-29 12:20
50--------------------------------2009-10-19 10:20
24--------------------------------2009-10-28 10:20
3----------------------------------2009-10-28 10:20 <-------- ( match!)
sheet: proximo pedido
a----------------------------------------b
4----------------------------------2009-10-28 10:20
20---------------------------------2009-10-29 13:00
3----------------------------------2009-10-19 15:20
24---------------------------------2009-10-29 13:40
3-----------------------------------2009-10-27 13:20 <------------ (example)
i wrote formula first conditioning vlookup , other index match, vlookup gave me last value of dates in checkinglist, , tried code : sub tempototal1()
dim checkinglist worksheet dim proximpedido worksheet dim tear1 range dim inicio range dim tear2 range dim saida range dim diferença range dim cell1 range dim cell2 range dim integer set tear1 = worksheets("checkinglist").range("a2").currentregion set inicio = worksheets("checkinglist").range("e2").currentregion set tear2 = worksheets("proximopedido").range("a1").currentregion set saida = worksheets("proximopedido").range("b2").currentregion set diferença = worksheets("proximopedido").range("c2").currentregion on error resume next each cell1 in tear1 if tear1.cells.value = tear2.cells.value each cell2 in inicio if tear2.cells.value > saida.cells.value diferença.cells.value = inicio.cells.value - saida.cells.value end if exit next cell2 end if exit next cell1 end sub
thanks
setup 2 variables in code
- the first track cell location
dim matchcell range
- the second track delta between source cell , match cell
dim targetdelta double
now, loop through data on sheet: proximo pedido performing following logical steps
- calculate absolute delta between date on current cell , and date in checklist
- if delta less current value of targetdelta
- then update delta new value , record current address in matchcell variable
- continue looping
- after loop, know cell closest - stored in matchcell variable
Comments
Post a Comment