2
votes

My VBA code is

Function yEval(entry As String)
yEval = Evaluate(entry)
Application.Volatile
End Function

in Sheet1 in cell f4 i have formula =yEval(Sheet2!E19) cell d4 on sheet1 contains number 12 Sheet2!I19 contains string $Q

if Sheet2!e19 has string INDIRECT("pries!"&Sheet2!I19&12) or INDIRECT("pries!"&Sheet2!I19&Sheet1!d4) or INDIRECT("pries!"&Sheet2!I19&"Sheet1!D"&row()+12) the formula return result from sheet pries!$Q12

if Sheet2!e19 has string INDIRECT("pries!"&Sheet2!I19&address(row(),4)) or INDIRECT("pries!"&Sheet2!I19&"Sheet1!D"&row()) the formula return result 0 it seems like ignoring the row() function

How to make work this formula to change d4 part depending on row number, if row is 5 then d5

INDIRECT("pries!"&Sheet2!I19&Sheet1!d4)
1

1 Answers

1
votes

Interesting problem... For further insight into the issue try these from the immediate or watch window:

?[indirect("rc",0)]
?[index(a:a,row())]
?[offset(a1,row()-1,column()-1)]

Strangely they all evaluate to A1 no matter which cell is active. It's a quirk of Evaluate or [] that references are treated relative to A1 instead of the active (calling) cell when the return value is a range reference. You can think of row() as equivalent to row(RC) in RC-notation evaluated relative to A1.

However, note that changing ] to &""] in the three formulas above results in an error in the first but the expected result in the last two so that row() is now evaluated relative to the active cell. So it seems that Evaluate is functioning differently when the return value is not a range reference.

Update

Based on these observations you can use INDEX for the row reference and move INDIRECT outside:

Sheet1!F4:=INDIRECT(yEval(Sheet2!E19))
Sheet2!E19:"pries!"&Sheet2!I19&INDEX(Sheet1!D:D,Row())

...or just use RC-style references without the need for the UDF:

=INDIRECT("pries!"&Sheet2!I19&INDIRECT("RC4",0))