Here's a way of doing it using C++ and VBA
http://excelmvf.sourceforge.net
I've implemented the same idea many times in pure VBA but without the
use of array formulas.
The basic idea is:
First time the UDF is called it spawns an object of a "calculation-
event listening" class that is initiated with:
the address of the calling cell,
the formula within the cell,
and the destination address to paste the results to.
If a generic class, the "function" plus associated arguments to
perform the task required would also be passed (in my case it's a SQL
statement) but you could of course have a different class for each
function.
The UDF having successfully spawned an object, returns an intermediate
value to the cell (I usually return 0).
The action of returning a value to the underlying cell fires the
calculate event which is picked you by the already in place object. If
the calculation applies to the object's assigned calling cell it does
whatever it should do (in my case select a table of data from a
database) and pastes the resulting data back to Excel. Remember this
is code independent of the UDF so the no-side-effects rule doesn't
apply, it can affect other cell values. When the data is pasted back,
the calling cell's ".formula" value is set to the original formula.
The object also locks itself i.e. will run only once and deletes
itself (or rather sets in motion the process by which the garbage
collector will do so).
The re-action of rewriting the cell's formula is to fire the cell a
second time. The UDF recognises this is a 2nd call and goes about
getting a result from the calling object (in VBA I use dictionary
objects keyed on the cell's full address to allow result values to be
passed back & forth and keep track of the calling sequence; this is
also the mechanism used to "keep alive" and subsequently de-commission
the spawned object). The UDF then returns this value as its value
(typically an indication of success or failure, the "real" data will
already have been pasted to another location by the now deceased
helper object)
Obviously for this to work, the workbook must be operating in auto-
calculate mode. Also, as others have said,this is against the spirit-
of-Excel's no-side-effects rule, but it does work!
Tom
On Oct 19, 3:38 pm, "
rlethbri...@gmail.com" <
rlethbri...@gmail.com>
wrote: