Excel VBA - Perform Operations on visible cells only -
i have database has in excess on 200,000 rows. when writing vba script had database of 20,000 rows in mind didn't care whether database filtered or not because vba script ran quickly. given realization database huge , testing vba script surprised notice how ran. without further how code looks :
set wsdb = thisworkbook.sheets("db") dim nameindex long: nameindex = application.match(name, wsdb.rows(1), 0) dim formula string formula = "=iferror(averageifs(" + gra(nameindex) + "," + gra(dateindex) + ",r2c," + gra(cellnameindex) + ",rc1" + "),"""")"
where gra function returns address of range of column.
private function gra(byref rngindex long) gra = "db!" + cstr(range(cells(2, rngindex), cells(rownos, rngindex)).address(1, 1, xlr1c1, 0, 0)) end function
so given filter table beforehand how can adjust code ignores hidden rows , takes account visible. of course aware simple dirty solution copy filter database , paste in new sheet affect performance i'm trying improve.
you can use following function return range of visible cells.
function visiblecells(rng range) variant dim r range dim arr() integer dim rndx long dim cndx long if rng.areas.count > 1 visiblecells = cverr(xlerrref) exit function end if redim arr(1 rng.rows.count, 1 rng.columns.count) rndx = 1 rng.rows.count cndx = 1 rng.columns.count set r = rng(rndx, cndx) if (r.entirerow.hidden = true) or _ (r.entirecolumn.hidden = true) arr(rndx, cndx) = 0 else arr(rndx, cndx) = 1 end if next cndx next rndx visiblecells = arr end function
the above code came http://www.cpearson.com/excel/visiblecells.aspx.
normally post code write thinking.
Comments
Post a Comment