excel - UDF as criteria for advanced filter within macro error -
if use udf generate criteria advanced filter, , run advanced filter using vba, 1004
error generated within udf.
if advanced filter called excel, filter functions expected.
why difference?
(and yes, know there other methods can used. trying understand difference between calling advanced filter excel vs vba when using udf criteria).
i filtering return entire row, if item in row has red font (rgb 255,0,0). udf within code below.
in screenshot below, criteria formula are:
a2: =isred(a8) b3: =isred(b8) c4: =isred(c8)
the screenshot shows advanced filter functioning designed when called excel
but when code below run, after column headers copied e1:g1
, code stops within udf above error message. @ time r.address
= a8
i tried recording code when did advanced filter excel, , executing recorded code instead of below. resulted in same error.
option explicit sub marine() dim rtable range dim rcriteria range dim rdestination range set rtable = range("a7").currentregion set rcriteria = range("a1:c4") set rdestination = range("e1") rdestination.resize(columnsize:=3).entirecolumn.clear rcriteria .rows(1).clearcontents .cells(2, 1).formula = "=isred(a8)" .cells(3, 2).formula = "=isred(b8)" .cells(4, 3).formula = "=isred(c8)" end rtable.advancedfilter action:=xlfiltercopy, criteriarange:= _ rcriteria, copytorange:=rdestination, unique:=false end sub function isred(r range) boolean 'runtime error '1004' on next line when above macro run isred = (r.font.color = rgb(255, 0, 0)) end function
this may not serve answer since can't comment here goes:
when calculating or running filter excel , looking @ locals window, breakpoint on 'isred =' line, of expressions defined, including currentarray formatconditions.
when running macro, breakpoint reached 3 times functions set, assuming automatic calculation. first time through, currentarray, directprecedents, precedents not defined, when there in regular function call. second time, displayformat not defined. third, displayformat. edit: these return false expected range reference different.
next time advanced filter applied 'unable xxxx ' seems main value, a4 come through formula , formulalocal. 1004 error.
my point: isred function , range reference seems behave differently when calculation happening part of macro running. provides impetus further investigation.
Comments
Post a Comment