excel - Search for value in column that matches another column AND a date? -
i have data stored in 3 columns of excel
column a: serial number column b: date column c: value (e.g. cost)
i need value (column c) associated particular serial number (column a) , date (column b).
so example, in screenshot below, if want value associated serial number (t455) , date (dec 13, 2010), value should 8.
the method can come computationally inefficient, because go through cells each time value.
is there method, example, limit search area given serial number?
for example, if looking value serial number t455, how can limit code search date in rows (6-13) , find corresponding value in column c, rather searching whole table?
sub findvalue() dim s string dim d date dim v integer s = t455 d = dec 13, 2010 = 1 range("a1").end(xldown).row if range("a" & i) = s , range("b" & i) < date - 7 , range("b" & i) < date + 7 ' way search date range rather specific date v = range("c" & i).value end if end sub
i thought of while loops, or lookup functions, reached dead end.
non-vba solution may lot easier , less of headache.
column consists of formula, a1 = "=b1&c1"
cell g1 formula can seen in formula bar.
update here vba solution work faster, there notes based on wrote unsure of. also, see comments code work more want to.
sub findvalue() dim s string, d date, v integer, rngfound range, cel range s = "t455" 'needs quotes around string d = "dec 13, 2010" 'needs quotes around date dim wks worksheet set wks = activesheet wks 'always better autofilter loop when can! .usedrange.autofilter 1, s .usedrange.autofilter 2, ">" & d - 7, xland, "<" & d + 7 set rngfound = intersect(.usedrange, .columns(3)).specialcells(xlcelltypevisible) 'the thing here if have date range _ 'you may return more 1 result _ 'in case, don't know want v's if not rngfound nothing each cel in rngfound v = cel.value next end if .autofiltermode = false end end sub
Comments
Post a Comment