+ Reply to Thread
Results 1 to 31 of 31

excel solver like Public Function

  1. #1
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    excel solver like Public Function

    Hi,

    I'm Trying to write a public function that is like the solver add-in in excel. The reason I cant use solver is because the solver add-in doesn't automatically recalculate as the values in the cells are changing. My spreadsheet is dynamic and the values are constantly changing.

    I have three values. These are in cells A1, A2, and A3. I call these value1, value2 and value3 respectively in the code below. Changing the value in A3 changes A1 but not A2. I would like a function that will give me the value of A3 for which A1-A2=0

    here is the code i have tried to write but it obviously isn't working. I'd appreciate any help on this. There is probably some simple loop type function that would do the trick. Please help.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: excel solver like Public Function

    The loop in your function doesn't do anything -- nothing changes, so it will loop forever.
    The reason I cant use solver is because the solver add-in doesn't automatically recalculate as the values in the cells are changing.
    I don't understand that.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: excel solver like Public Function

    There are many different algorithms for finding roots of equations. How familiar are you with numerical methods? Which numerical method would you use? What kind of function is in A2?


    Newton-Raphson method is pretty easy to program when there's only one variable changing, assuming the function can be readily differentiated.
    A bisection type algorithm is another that I have used that isn't too difficult to program, but you have to be able to give the algorithm two starting points, one on each side of the root.

  4. #4
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    Hi Shg,

    to use the solver function one has to manually click on the solver button in the excel ribbon and recalculate. Plus on any given spreadsheet you can only use solver for a single set of cells. That is why i would like to have a function that i can use throughout the spreadsheet.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: excel solver like Public Function

    to use the solver function one has to manually click on the solver button in the excel ribbon and recalculate.
    You can automate solver. There are many examples in this forum, and one from a few minutes ago at http://www.mrexcel.com/forum/showthread.php?t=638727. You can't automate solver in a UDF (a function called from the worksheet), but you could via a macro triggered by a change event.
    Plus on any given spreadsheet you can only use solver for a single set of cells.
    Solver remembers the last model used on each sheet.

    EDIT: And you can store and reuse as many models on a sheet as you wish.
    Last edited by shg; 05-30-2012 at 03:34 PM.

  6. #6
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    let say the range of values for value3 could be .01 - 1

  7. #7
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    how would this work with the bisection method? could you give an example code?

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: excel solver like Public Function

    quick and untested pseudocode, for 2^-10 precision
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    how would this work for an unknown function F(value3) that produces value1?

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: excel solver like Public Function

    Off the top of my head, I don't think it can. Any root finding algorithm that I can think of needs to be able to calculate F. If you code it right, you can make F as generic as you need to, but I think you are going to need to put something in the code so it can calculate F.

  11. #11
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    what do u mean when u say "make F as generic as you need to"...if the function is actually some complicated formula but when i code it i enter F(x)= 2*x .... it wouldn't work would it?

  12. #12
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: excel solver like Public Function

    what do u mean when u say "make F as generic as you need to".
    That means that you code F so that you can pass necessary parameter to it. For example, if you are working with polynomials (F(x)=A+Bx+Cx^2+Dx^3+...), you can pass the parameters A,B,C,D,... to the function rather than hard coding those values into the code.

    if the function is actually some complicated formula but when i code it i enter F(x)= 2*x .... it wouldn't work would it?
    I don't see why not. Of course you can't code it in algebraic syntax, you have to use VBA syntax. I got the impression from the 1st post that you had already figured out how to code the function into a spreadsheet cell. Call me optimistic, but I figure any function that you can code into a spreadsheet cell can be adapted into one or more lines of VBA code.

  13. #13
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    can the address, match and index functions from an excel spreadsheet be used in a VBA code? im trying to enter into vba code Application.Worksheet.Function.Address(2, 3) but it doesn't seem to work...is it because some excel functions cannot be used in VBA? How do i know which ones can and which ones cannot?

    thanks

  14. #14
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: excel solver like Public Function

    You can access worksheet function through the Worksheetfunction property (Application.worksheetfunction.match(arguments). I thought there was a list of worksheet functions available to VBA in the VBA help files. Try putting something like worksheet functions available to VBA in the VBA search engine and see if it comes up.

    The match function is commonly called from VBA. The others, not so much, probably because the .cells(i,j) property/method usually works just as well without needing the worksheet function version.

  15. #15
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    I have a function that has 3 variables ( f(x,y,z) = 2X + y^2 + 1/z ) that i would like to compare to a set of values that i will call "observed values f(obs)"... i would like to find the values for x, y, and Z that will minimize the sum of squares....

    please see the attachment for the equation

    minimize.JPG

    how do i do this optimization problem?
    just to be clear...f(obs) are just values listed in Column A...like 1.5, 1.6, 1.8, 1.9, 2.5, 3.6, 5.4, 8, 9.1 ...etc

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: excel solver like Public Function

    You're missing something fundamental.

    You have one set of observations, and want to derive three hidden variables, x, y, and z, given the function f = 2x + y^2 + 1/z.

    So assume y=z=1; then x = (obs-2)/2

    Or assume x=z=1; then y = sqrt(obs-3)

  17. #17
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    lets say we had only two observed values...2.5 and 3.5

    the question is ....for what values of x,y,and z would we minimize.... [2.5 - (2X + y^2 + 1/z )]^2 + [(3.5 - (2X + y^2 + 1/z )]^2

    please let me know if this makes sense

  18. #18
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: excel solver like Public Function

    The result must be f=3, right?

    So pick any values you like for two of the variables (z<>0), and solve for the remaining one.

  19. #19
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    Attachment 159330

    please have a look at the attached file with example...

    i can do this with solver but i need to do this in realtime..which is why im trying to have vba code for this...

    if u use solver and use the following settings....target cell C6, equal to "min", and by changing cells $E$3:$G$3....u get an answer

  20. #20
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: excel solver like Public Function

    "Invalid attachment"

  21. #21
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    as long as the initial value of z is not zero this works in solver....however i need to automate this so i don't have to do this manually using solver because the observed values will change in real time in my actual sheet

  22. #22
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    how do i attach a file to this thread?

  23. #23
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    example sheet

    exampleoptimize.xlsx

    attached spreadsheet

  24. #24
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function


  25. #25
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: excel solver like Public Function

    i have posted the attachment on a new thread...the link is above , thats the only way it was allowing me to attach a spreadsheet

  26. #26
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: example sheet

    http://www.excelforum.com/excel-prog...unction-2.html

    the attachment if for the thread at the above link...

  27. #27
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: excel solver like Public Function

    There is nothing to automate until you understand that two of the variables are redundant.

    Change E3 to 0.5 set F3 and G3 to 1. Same result.


    Change E3 and F3 to 0, and set z =1/3. Same result.
    Last edited by shg; 06-01-2012 at 04:36 PM.

  28. #28
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: example sheet

    why are you putting it in a new thread. You need to upload the example in the original thread.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  29. #29
    Registered User
    Join Date
    10-20-2010
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: example sheet

    How do u attach a file to an existing thread? I only see the attachment option when I start a new thread

  30. #30
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: excel solver like Public Function

    I have moved your attachement now...
    TO upload to existing file click Edit post -> Go Advanced and push paper clip button

  31. #31
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: excel solver like Public Function

    Thanks zbor

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1