vba - Excel custom formula with IF statements and INDEX, MATCH returns a #VALUE error -


i extracting values 1 excel sheet another. use following formula:

= if(isblank(ifna(index(range1;row;match(dates;range2;0));error1));error2;ifna(index(range1;row;match(dates;range2;0));error1)) 

since need calculations extracted data wanted create custom function simplify extraction process eliminating need type parameters stay same every time. built following function:

function datacap(dates variant, row variant) variant     dim range1 range 'define whole lookup table        set range1 = range("data!a1:p151")     dim range2 range 'define date lookup table        set range2 = range("data!a3:p3")     dim error1 range 'define error 1; if no value        set error1 = range("x16")     dim error2 range 'define error 2; if blank        set error2 = range("y16")     datacap.formula "= if(isblank(ifna(index(range1;row;match(dates;range2;0));error1));error2;ifna(index(range1;row;match(dates;range2;0));error1))"   end function 

when run #value error. if paste equation (with same arguments) directly in cell, works expected. so, think problem vba not able calculation. suggestions how fix this?

thank in advance

without being able test on data can't sure if work, might @ least able point more in right direction.

public function dcap(xdates variant, xrow variant) dim range1 range, range2 range, error1 range, error2 range  set range1 = activeworkbook.sheets("data").range("a1:p151") set range2 = activeworkbook.sheets("data").range("a3:p3")  set error1 = activeworkbook.activesheet.range("x16") set error2 = activeworkbook.activesheet.range("y16")  on error resume next if iserror(worksheetfunction.index(range1, xrow, worksheetfunction.match(xdates, range2, 0)))     dcap = error1 elseif len(worksheetfunction.index(range1, xrow, worksheetfunction.match(xdates, range2, 0))) = 0     dcap = error2 else     dcap = worksheetfunction.index(range1, xrow, worksheetfunction.match(xdates, range2, 0)) end if on error goto 0  end function 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -