excel - How to build a dynamic cell range output function using VBA -
i building function, when select cell, output cell range selection last filled cell row on same column.
here code, works perfectly.
''get cell range selection last cell function cellrange(cella range) cellrange = cella.address + ":" + cella.end(xldown).address end function
question: want update code, when used dates, user can filter 3 options: ytd (year date), (all time - i.e. getting data), year (i.e. 2015 / 2014 / 2013 etc.)
my end goal user select cell in range column of dates , input ytd or all or given year (i.e. 2014) , range filter.
example: user writes =cellrange(a2,2014)
, should yield $a$2:$a$23
, if user changes =cellrange(a2,2014)
should yield $a$24:$a$40
seen on image.
i tried various loops or counts feel quite lost none of tries apparently made sense.
i looking help: guidance or solution problem preferably, want build on after tackle 1 (hence why doing on vba).
here shorter solution works 3 scenarios, , not require data worksheet active:
public function cellrange(rdates range, vfilter variant) string dim long, ndx1 long, ndx2 long, r range, va variant, berr boolean, ball boolean berr = true if isdate(rdates) rdates.entirecolumn = rdates.parent.evaluate("count(" & .address & ")") set r = .cells(1 - + rdates.parent.evaluate("index(" & .address & ",match(9.9e+307," & .address & "))").row).resize(i, 1) end va = r.value select case lcase(vfilter) case "all": berr = 0: ball = 1 case "ytd" = 1 ubound(va) if ndx1 = 0 , year(va(i, 1)) = year(date) ndx1 = if va(i, 1) <= date ndx2 = next case else 'year vfilter = val(vfilter) if vfilter = 1 ubound(va) if ndx1 = 0 , year(va(i, 1)) = vfilter ndx1 = if ndx1 , year(va(i, 1)) = vfilter ndx2 = next end if end select if not ball if ndx1 > 0 , ndx2 > 0 set r = r.range(r.parent.cells(ndx1, 1), r.parent.cells(ndx2, 1)): berr = false if not berr cellrange = r.address else cellrange = cverr(xlerrvalue) end if end function
Comments
Post a Comment