+ Reply to Thread
Results 1 to 14 of 14

How to prevent VBA function calls from reevaluting when worksheet opens up

  1. #1
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    How to prevent VBA function calls from reevaluting when worksheet opens up

    I have some VBA functions which print a string on the cell from where it's called.

    For e.g.

    Please Login or Register  to view this content.
    Now let's say I call this from Cell A1 as "=PrintStuff()", then you see either Lesser or Not Lesser in Cell A1

    Now I call this function & other similar functions many, many times in my worksheet.

    What I want is that once the function call is evaluated & something is written on to the cell, it becomes permanent. i.e. it doesn't reevaluate the function everytime the sheet is opened - is this possible

    Why I want this is because sometimes when I reopen the sheet, Lesser gets changed to Not Lesser & vice versa. This is inspite of the cells which the function checks not changing. I clear out the formula call from the cell, & rewrite the call, the string goes back to what was originally being printed.

    So is there a way to freeze the output of a VBA function call?
    Last edited by ExcelBoy90; 01-21-2020 at 03:41 AM.

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,447

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)

  3. #3
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    Quote Originally Posted by Pepe Le Mokko View Post
    you need to include code tags around your code.
    I have added code tags now

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    I can't imagine why you would use a UDF for that, but it shouldn't recalculate even if you change the values of the cells it's looking at, since they aren't passed in as direct arguments and you didn't make the function volatile.
    Rory

  5. #5
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    Quote Originally Posted by rorya View Post
    I can't imagine why you would use a UDF for that

    What else could I use for that?

    Quote Originally Posted by rorya View Post
    but it shouldn't recalculate even if you change the values of the cells it's looking at, since they aren't passed in as direct arguments and you didn't make the function volatile.
    Yeah, I know - but I given up trying to find a good repro case - it seems to happen very randomly not all the time.

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    What’s wrong with a simple if formula?

  7. #7
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    Quote Originally Posted by rorya View Post
    What’s wrong with a simple if formula?
    The actual function is a little more complex than the sample I showed above.
    It loops through cells before deciding which cells to get Val1 & Val2

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    I'd imagine you could still do that with built-in functions. Even if you do use a UDF, you should really pass the ranges as arguments rather than using Offset in your code.

  9. #9
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    Quote Originally Posted by rorya View Post
    I'd imagine you could still do that with built-in functions.
    How would you loop in built in functions?

    Quote Originally Posted by rorya View Post
    Even if you do use a UDF, you should really pass the ranges as arguments rather than using Offset in your code.
    The range is not known. The range is also figured out in the UDF by looping & searching for a cell with a particular value.

  10. #10
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    You'd use arrays or functions that find values. Impossible to be specific when you haven't given any real detail.

    If your UDF loops, it presumably loops through a range. That is what should be passed as the argument(s).

  11. #11
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    Quote Originally Posted by rorya View Post
    If your UDF loops, it presumably loops through a range. That is what should be passed as the argument(s).
    It's not a fixed range, the range also is figured out by traversing till it finds a cell with a particular value. Why does it need to be passed as an argument? If I can figure out the range programmatically inside the function, why should I do the visual, possible error prone task of doing the same manually before calling the function?

  12. #12
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    If you create a sample data set (maybe 10 or 20 lines, with your complete/ current/ mostly working code) and upload it, we may be able to give you a better solution than you expect.
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  13. #13
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    Quote Originally Posted by ExcelBoy90 View Post
    It's not a fixed range, the range also is figured out by traversing till it finds a cell with a particular value. Why does it need to be passed as an argument?
    If you don't pass it as an argument, you should make the function volatile, since otherwise it won't react to changes of the data in the range. Making UDFs volatile should generally be a last resort.

    If I can figure out the range programmatically inside the function, why should I do the visual, possible error prone task of doing the same manually before calling the function?
    It appears that you are wedded to your current approach, so there's little need to address how a formula approach could work.

  14. #14
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    Re: How to prevent VBA function calls from reevaluting when worksheet opens up

    Quote Originally Posted by rorya View Post
    If you don't pass it as an argument, you should make the function volatile, since otherwise it won't react to changes of the data in the range. Making UDFs volatile should generally be a last resort.
    I don't want it to react at all after first evaluation. That's my original question if you look at my post - my cells seem to be reevaluating when I don't want them to.
    Quote Originally Posted by rorya View Post
    It appears that you are wedded to your current approach, so there's little need to address how a formula approach could work.
    It doesn't make sense to change the current approach without any compelling reason. If anything, your approach would make my original issue worse. And your approach is error prone. I call this function 100 times in one worksheet. I am sure make a mistake occasionally in eyeballing it and passing the right range.

+ 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. Replies: 2
    Last Post: 07-05-2019, 11:47 PM
  2. [SOLVED] How to show a VBA function's output in the same worksheet cell that calls it?
    By geophysicist in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-13-2013, 08:33 AM
  3. [SOLVED] GCD - subprogram w/o GCD built in Function
    By ChemEBA in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-12-2013, 03:54 PM
  4. prevent run time error when excel opens in protected mode
    By draddy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-14-2013, 12:34 PM
  5. Prevent editing:opens automatically
    By pmguerra in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-23-2010, 07:58 AM
  6. VBA & Built-in function?
    By sanlen in forum Excel General
    Replies: 1
    Last Post: 07-29-2010, 03:40 AM

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