+ Reply to Thread
Results 1 to 10 of 10

Nested Application.Evaluate not working

  1. #1
    Registered User
    Join Date
    06-18-2015
    Location
    Austria
    MS-Off Ver
    2010
    Posts
    15

    Nested Application.Evaluate not working

    Hello,
    I've encountered a quite specific problem with the Application.Evaluate method. Apparently nested calls to it do not work (code execution stops without any message). I've attached a simple file that pinpoints the problem. Execution of "Sub test" in the Sheet1 object should display a MsgBox showing "10". However, nothing happens. I'm using Excel 2010.
    Any help would be appreciated.
    Attached Files Attached Files

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Nested Application.Evaluate not working

    Hi kaktus018,
    try it
    Please Login or Register  to view this content.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Nested Application.Evaluate not working

    You need to change this:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Nested Application.Evaluate not working

    In fact, these all do the same thing, although that wasn't the point of the exercise.

    Please Login or Register  to view this content.

    Evaluate is normally used to, well, evaluate a "formula". For example:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-18-2015
    Location
    Austria
    MS-Off Ver
    2010
    Posts
    15

    Re: Nested Application.Evaluate not working

    Thanks for the quick replies. I know that Evaluate is not necessary for the posted sample code to work. It is just meant to show the problem I want to solve.
    I've written a numeric modul in VBA and recently added an optimization algorithm. Now this algorithm needs to evaluate an objective function (let it be fun1) and find the minimum of it. For that purpose the method needs a function-String which is then evaluated using Application.Evaluate (it can also be something like "x^2+y^2", no need to be a UDF this way). This works fine as long as there are no further calls to Evaluate inside fun1.
    Now I want to solve a differential equation inside fun1. My solver uses the same strategy here - it evaluates a function-String with Application.Evaluate. So fun1 is called by Evaluate and itself uses Evaluate again. This means a nested structure and is exactly my problem.
    Is there no way to make it work that way?

  6. #6
    Registered User
    Join Date
    06-18-2015
    Location
    Austria
    MS-Off Ver
    2010
    Posts
    15

    Re: Nested Application.Evaluate not working

    I now came up with a workaround. Directly evaluating the string in a cell seems to work with Application.Evaluate. So i wrote a function that adds a temporary sheet to the workbook and uses cell "A1" to evaluate the string. It then returns the result of the evaluation. This way I can implement it the same way as Application.Evaluate.
    Here is the function:
    Please Login or Register  to view this content.
    The following sub deletes the temporary sheet after computation is completed:
    Please Login or Register  to view this content.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Nested Application.Evaluate not working

    I still don't understand why you need to use Evaluate at all. If you have a function, why not just execute it and return a value. If you have values in cells, why not just carry out the calculation in code or evaluate the formula?

    Whatever, you have a solution that meets your needs.

  8. #8
    Registered User
    Join Date
    06-18-2015
    Location
    Austria
    MS-Off Ver
    2010
    Posts
    15

    Re: Nested Application.Evaluate not working

    Well, lets say I've got the function "x^2" I want to integrate. So I pass the string "x^2" on to my solver and tell it that "x" is the variable. The solver than replaces "x" at every calculation point with a number and evaluates the function.
    The function string can of course also contain a UDF. That way I don't need to explicitly implement "x^2" or any other function in a VBA function (and I don't know how Application.Run would handle nested calls).

  9. #9
    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: Nested Application.Evaluate not working

    I'm not sure I understand the issue. Here's an example of integrating a function that includes a UDF:

    A
    B
    2
    1.0919
    A2: =Integral("myUDF(x)*sin(x)", 1, 2, 50)


    Please Login or Register  to view this content.
    And here's the integration code:

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Registered User
    Join Date
    06-18-2015
    Location
    Austria
    MS-Off Ver
    2010
    Posts
    15

    Re: Nested Application.Evaluate not working

    This code as well uses Evaluate. I haven't tried it but I suppose when there is another call to Evaluate by myUDF, the code won't work.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Application.Evaluate not working - Array Formula
    By cmore in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-28-2015, 01:12 AM
  2. [SOLVED] Application.Evaluate error when called with a button
    By emp1346 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-28-2014, 09:59 AM
  3. Workaround for Error 2015 when function too long using Application.Evaluate?
    By jprealini in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-18-2014, 04:06 PM
  4. [SOLVED] SUMIF function don't return same result with Evaluate and Application.WorksheetFunction
    By zbor in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-31-2012, 12:48 PM
  5. VBA - Strange behavior with Application.Evaluate and Sumproduct
    By largebrick in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-31-2010, 07:04 AM
  6. [SOLVED] Error 2015 with Application.Evaluate
    By Jeff in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2006, 11:10 AM
  7. Nested IF to evaluate street addresses
    By ManosS in forum Excel General
    Replies: 6
    Last Post: 03-16-2006, 11:10 AM

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