So here's what I have been able to find so far: setting IsMacroType=true seems to solve the issue (I`m not entirely sure, because the error is somewhat hard to reproduce).
However, I have now run into a calculation problem that is extremely dangerous, because it`s very silent.
Just in case it`s not clear, the primary goal of the UDF in question is to serve as semantic suger for excel formulas that look like OFFSET(VALUES_RANGE, MATCH(LOOKUP_ID,IDS_RANGE,0)-1,0,1,1), which is a much better practice than to use the VLOOKUP function, eliminating the fragile LOOKUP_COLUMN parameter and allowing to search for values even when the IDS_RANGE is not exactly to the left of the VALUES_RANGE.
So here`s the scenario that I have. I have a table with several columns that come from an external data, and 3 columns that are created in excel: GROUP_ID, ITEM_PROPERTY and GROUP_PROPERTY.
GROUP_ID is entered by hand.
ITEM_PROPERTY is a concatenation of the GROUP_ID with other columns in the table
GROUP_PROPERTY is the formula TestLookup([@GROUP_ID];[GROUP_ID];[ITEM_PROPERTY];FALSE)
Note that GROUP_ID is not unique. What I`m trying to do is to get the ITEM_PROPERTY of the first element of the given group.
Now suppose that you have the following data:
GROUP_ID, ITEM_PROPERTY, GROUP_PROPERTY
1, 1-a, 1-a
1, 1-b, 1-a
1, 1-c, 1-a
2, 2-d, 2-d
2, 2-e, 2-e
And now we change the GROUP_ID in the third row to 3. Suprinsingly, we get the following results:
1, 1-a, 1-a
1, 1-b, 1-a
3, 3-c, 1-c
2, 2-d, 2-d
2, 2-e, 2-e
Note that the correct result for the formula should be "3-c". "1-c" isn't even present on the worksheet anymore!
What I'm working out from this is that, when previously calculating the value of cell [3,3], Excel only registered the dependance to cell [1,2], which is the one where GetValue() was called. That's despite the fact tath the whole ITEM_PROPERTY column was passed on to the function.
So now, when processing the change on cell [3,1], the first thing it does is to fire up the call to the TestLookup function, getting the value "1-c". It's only after that the cell [3,2] is updated to "3-c".
This behaviour is found on a specific workbook, when the IsMacroType property is set to true. It is independent of the IsThreadSafe attribute. The biggest trouble is that I have been unable to reproduce this problem on a small standalone workbook, probably because of the complex calculating chain of the rest of the original workbook.
This problem has put me between a rock and hard place. IsMacroType=true seems to solve the original #VALUE problem. However, it creates this other unexpected behaviour, which is actually much worse because it can be very silent.
Do you have any thoughts on how to keep the calculation chain intact? Is there any way to make Excel understand that my formula depends on the entire range, without the need to actually get it's entire value (which would eliminate the performace gains I have been getting with ExcelDNA over VBA)?
Best regards,
Carlos Jourdan