PC Review


Reply
Thread Tools Rate Thread

Can UDF modify worksheet(s)?

 
 
curiousgeorge408@hotmail.com
Guest
Posts: n/a
 
      4th Feb 2008
I want to write a VB function (not a sub) that does something similar
to Solver (but nothing nearly as sophisticated or generalized) insofar
as it finds the "best" result by trial and error. The algorithm is
tailored to the purpose of the worksheet.

Ideally, the function would modify some cells in the active worksheet
(identified by name or passed as arguments) and let Excel automatic
calculation derive the result in a "target" cell, which depends on the
modified the cells directly or indirectly, just as if I were manually
modifying the cells manually. (Ideally, the "target" cell is the same
cell that contains the function call.)

This works as a macro (sub). But it appears that a UDF (function) is
not permitted to modify cells of either the active worksheet or even
another worksheet.

I just want to confirm that this cannot be done in a UDF. Or is there
some way that I can do it?

I know that I could duplicate all of the formula calculations within
the UDF. But I do not want to hardcode the formulas in VB. Instead,
I want the flexibility of changing the formulas in one place, namely
the worksheet.

I believe I could use Evaluate to execute each formula within the VB
function. I will work with that idea. But I am concerned that it
will be a lot slower than Excel.

Are there good alternatives that I have overlooked?
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      4th Feb 2008
UDFs cannot directly change cells, they can only return a value. A UDF can
change multiple cells with the help of an Event Macro.

Public triggger As Boolean
Public carryover As Variant
Function reallysimple(r As Range) As Variant
triggger = True
reallysimple = r.Value
carryover = r.Value / 99
End Function


In worksheet code:


Private Sub Worksheet_Calculate()
If Not triggger Then Exit Sub
triggger = False
Range("C1").Value = carryover
End Sub


Whenever reallysimple is called it returns a value. It also sets the global
flag triggger and the global variable carryover.


Now the event macro runs whenever the worksheet is calculated. As soon as
it sees that triggger has become true, it knows that reallysimple has been
executed and there is work to do. It clears triggger and moves carryover to
cell C1.


So even though a UDF can only directly change a single cell, it can
indirectly change many cells (with a little help from its friends).



--
Gary''s Student - gsnu2007d


"(E-Mail Removed)" wrote:

> I want to write a VB function (not a sub) that does something similar
> to Solver (but nothing nearly as sophisticated or generalized) insofar
> as it finds the "best" result by trial and error. The algorithm is
> tailored to the purpose of the worksheet.
>
> Ideally, the function would modify some cells in the active worksheet
> (identified by name or passed as arguments) and let Excel automatic
> calculation derive the result in a "target" cell, which depends on the
> modified the cells directly or indirectly, just as if I were manually
> modifying the cells manually. (Ideally, the "target" cell is the same
> cell that contains the function call.)
>
> This works as a macro (sub). But it appears that a UDF (function) is
> not permitted to modify cells of either the active worksheet or even
> another worksheet.
>
> I just want to confirm that this cannot be done in a UDF. Or is there
> some way that I can do it?
>
> I know that I could duplicate all of the formula calculations within
> the UDF. But I do not want to hardcode the formulas in VB. Instead,
> I want the flexibility of changing the formulas in one place, namely
> the worksheet.
>
> I believe I could use Evaluate to execute each formula within the VB
> function. I will work with that idea. But I am concerned that it
> will be a lot slower than Excel.
>
> Are there good alternatives that I have overlooked?
>

 
Reply With Quote
 
FSt1
Guest
Posts: n/a
 
      4th Feb 2008
hi
confirmed. UDF and other built in functions return values, they cannot
perform actions like modify anything.
my suggestion is to stick with solver.

Regards
FSt1

"(E-Mail Removed)" wrote:

> I want to write a VB function (not a sub) that does something similar
> to Solver (but nothing nearly as sophisticated or generalized) insofar
> as it finds the "best" result by trial and error. The algorithm is
> tailored to the purpose of the worksheet.
>
> Ideally, the function would modify some cells in the active worksheet
> (identified by name or passed as arguments) and let Excel automatic
> calculation derive the result in a "target" cell, which depends on the
> modified the cells directly or indirectly, just as if I were manually
> modifying the cells manually. (Ideally, the "target" cell is the same
> cell that contains the function call.)
>
> This works as a macro (sub). But it appears that a UDF (function) is
> not permitted to modify cells of either the active worksheet or even
> another worksheet.
>
> I just want to confirm that this cannot be done in a UDF. Or is there
> some way that I can do it?
>
> I know that I could duplicate all of the formula calculations within
> the UDF. But I do not want to hardcode the formulas in VB. Instead,
> I want the flexibility of changing the formulas in one place, namely
> the worksheet.
>
> I believe I could use Evaluate to execute each formula within the VB
> function. I will work with that idea. But I am concerned that it
> will be a lot slower than Excel.
>
> Are there good alternatives that I have overlooked?
>

 
Reply With Quote
 
Alan Beban
Guest
Posts: n/a
 
      4th Feb 2008
FSt1 wrote:
> hi
> confirmed. UDF and other built in functions return values, they cannot
> perform actions like modify anything.
> my suggestion is to stick with solver.
>
> Regards
> FSt1
>


More precisely, functions called directly from a worksheet can't modify
the Excel environment; if called by a Sub procedure, they can.

Alan Beban
 
Reply With Quote
 
FSt1
Guest
Posts: n/a
 
      5th Feb 2008
not in the sense i was speaking. when used in code, they are(to me) no longer
true worksheet functions but as part of the code. but that is getting off the
subject.

regards
FSt1

"Alan Beban" wrote:

> FSt1 wrote:
> > hi
> > confirmed. UDF and other built in functions return values, they cannot
> > perform actions like modify anything.
> > my suggestion is to stick with solver.
> >
> > Regards
> > FSt1
> >

>
> More precisely, functions called directly from a worksheet can't modify
> the Excel environment; if called by a Sub procedure, they can.
>
> Alan Beban
>

 
Reply With Quote
 
Alan Beban
Guest
Posts: n/a
 
      5th Feb 2008
FSt1 wrote:
> not in the sense i was speaking. when used in code, they are(to me) no longer
> true worksheet functions but as part of the code. but that is getting off the
> subject.
>
> regards
> FSt1
>
> "Alan Beban" wrote:
>
>
>>FSt1 wrote:
>>
>>>hi
>>>confirmed. UDF and other built in functions return values, they cannot
>>>perform actions like modify anything.
>>>my suggestion is to stick with solver.
>>>
>>>Regards
>>>FSt1
>>>

>>
>>More precisely, functions called directly from a worksheet can't modify
>>the Excel environment; if called by a Sub procedure, they can.
>>
>>Alan Beban
>>

UDF's are not worksheet functions. They are Function procedures that can
be called with VBA code or from a worksheet.

Alan Beban
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to modify a primary worksheet linked to another worksheet mamorellana Microsoft Excel New Users 7 23rd Jun 2009 02:32 PM
Modify a Worksheet oberon.black Microsoft Excel Programming 1 7th Sep 2005 12:32 AM
worksheet tab position--possible to modify it? =?Utf-8?B?emh1Z2U=?= Microsoft Excel Programming 1 22nd May 2005 02:58 PM
trying to modify formula for worksheet =?Utf-8?B?amVmZg==?= Microsoft Excel Worksheet Functions 2 1st Feb 2004 03:41 AM
Can't modify parts of a worksheet Paul Kraemer Microsoft Excel Misc 0 6th Nov 2003 03:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:11 AM.