+ Reply to Thread
Results 1 to 14 of 14

How do you restart/reset a Function?

  1. #1
    Forum Contributor
    Join Date
    05-16-2007
    Location
    USA
    MS-Off Ver
    MS Office 2016, Excel 2016
    Posts
    214

    How do you restart/reset a Function?

    I just created my first VB script...ok so I copied and pasted one I found on the internet.

    It works great for what I need, but my question is:

    How do you tell it to restart/reset the function?

    Here is what I have:

    Please Login or Register  to view this content.
    Last edited by FDibbins; 02-04-2015 at 05:54 PM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: How do you restart/reset a Function?

    If you mean, how do you use/activate it again, probably the easiest is to create a button and assign the macro to it
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Contributor
    Join Date
    05-16-2007
    Location
    USA
    MS-Off Ver
    MS Office 2016, Excel 2016
    Posts
    214

    Re: How do you restart/reset a Function?

    Quote Originally Posted by FDibbins View Post
    If you mean, how do you use/activate it again, probably the easiest is to create a button and assign the macro to it
    You can create buttons in Excel, I thought that was in Access?

    But yes, I need the function I created, StaticRand to generate another random number on command or via some method. Pressing F9 doesn't help, since it is now a non-volatile function.
    For now I have to close out of Excel and reopen the workbook to have it generate a new number.

    Thanks

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: How do you restart/reset a Function?

    Go to Insert Tab/Shapes/pick 1/set size in sheet
    right-click into teh shape and give it some text
    right-click again, and Assign the macro

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: How do you restart/reset a Function?

    Go to Insert Tab/Shapes/pick 1/set size in sheet
    right-click into teh shape and give it some text
    right-click again, and Assign the macro

  6. #6
    Forum Contributor
    Join Date
    05-16-2007
    Location
    USA
    MS-Off Ver
    MS Office 2016, Excel 2016
    Posts
    214

    Re: How do you restart/reset a Function?

    Quote Originally Posted by FDibbins View Post
    Go to Insert Tab/Shapes/pick 1/set size in sheet
    right-click into teh shape and give it some text
    right-click again, and Assign the macro
    Thanks for the detail steps, though unless I'm doing something wrong, it wants me to RECORD a macro when I click Assign Macro. It also has a button labeled New. I clicked the New button and pasted my StaticRand function into it, but when I click my action button I created earlier, I get a Compile Error: Expected End Sub. So I tried changing the last End Function to End Sub hoping that might fix it. Nope!

    Please Login or Register  to view this content.
    Is this because my StaticRand function is in VBA?

    Thanks again
    Mike

  7. #7
    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: How do you restart/reset a Function?

    Going back to your original question, with a formula in a cell,

    =randstatic()

    ... do Ctrl+Alt+F9 to force recalculation
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: How do you restart/reset a Function?

    Quote Originally Posted by Modify_inc View Post
    How do you tell it to restart/reset the function? Here is what I have:
    Please Login or Register  to view this content.
    Quote Originally Posted by Modify_inc View Post
    I need the function I created, StaticRand to generate another random number on command or via some method. Pressing F9 doesn't help, since it is now a non-volatile function.
    To minimize recalculations, you can select the Excel cell that references StaticRand, press F2, then press Enter.

    In effect, you are "re-editing" the cell. But since you simply press Enter, nothing changes. Nevertheless, Excel recalculates the cell (it calls the function) and any directly and indirectly dependent cells.

    PS.... To ensure a random sequence of numbers, you should call Randomize at least once (and preferrably only once) some time after opening the Excel file.

    One way is to create a Workbook_Open event macro. In VBA, press ctrl+R to open the Project Explorer pane on the left, double-click on ThisWorkbook, then copy the following and paste into VBA:
    Please Login or Register  to view this content.
    Alternatively, change your VBA function as follows:
    Please Login or Register  to view this content.
    Technically, the two are not the same. If VBA is reset (there are several ways that happens), Randomize will be called again. But that should be infrequently and "randomly" enough that it should not adversely affect the sequence of random numbers.

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

    Re: How do you restart/reset a Function?

    One could simply add application.volatile to the function. It will then be volatile like Excel's built in random number functions, and should generate a new random number with each calculate event. Or do you need more control over when this function recalculates?

    Of course, at that point, you could simply have =INT(RAND()*617)+1 in a cell, and completely bypass the VBA UDF. This makes me wonder if there is more to the question. Specifically, how to control when a UDF recalculates.

    One thing I have sometimes done is had a "dummy argument" in the UDF's argument list. This dummy argument is not used in the function, but it allows you to put a reference to another cell in the function call. Any time you change/edit/calculate or do anything to the dummy reference will force the function to recalculate.

    Please Login or Register  to view this content.
    Then you can "tell" Excel to calculate the UDF by entering something into A1. I could even see creating a checkbox that links to A1. Then, whenever you change the state of the checkbox, the UDF will recalculate. See "Add a checkbox, etc. to a worksheet" in Excel help.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  10. #10
    Forum Contributor
    Join Date
    05-16-2007
    Location
    USA
    MS-Off Ver
    MS Office 2016, Excel 2016
    Posts
    214

    Re: How do you restart/reset a Function?

    Quote Originally Posted by MrShorty View Post
    Of course, at that point, you could simply have =INT(RAND()*617)+1 in a cell, and completely bypass the VBA UDF. This makes me wonder if there is more to the question. Specifically, how to control when a UDF recalculates.
    I have created a test that generates the questions from a list. The questions are chosen randomly and the answers are calculated when the user presses enter. This doesn't work very well with the volatile nature of Excel's Rand() function because when you press enter to have it determine if your answer is correct, the question is changed (randomized again automatically) thus your answer is wrong every time.

    So using the StaticRand UDF, I can answer all the questions and get the results without the questions changing. But now I need a way to refresh the questions, reset/restart the StaticRand function on demand. The suggestion FDibbins proposed seemed the ideal way. I create a button, assign a Macro, label it Refresh, and when you click it, it would refresh the test questions or rerun the StaticRand function on demand. Unfortunately though, the Assign Macro option doesn't appear to work like that.

    For now I can close Excel and reopen it to regenerate my test questions, which is silly, but it works.

    I will try the "dummy argument" though.

    Thanks
    Mike

  11. #11
    Forum Contributor
    Join Date
    05-16-2007
    Location
    USA
    MS-Off Ver
    MS Office 2016, Excel 2016
    Posts
    214

    Re: How do you restart/reset a Function?

    Quote Originally Posted by joeu2004 View Post
    To minimize recalculations, you can select the Excel cell that references StaticRand, press F2, then press Enter.

    In effect, you are "re-editing" the cell. But since you simply press Enter, nothing changes. Nevertheless, Excel recalculates the cell (it calls the function) and any directly and indirectly dependent cells.

    PS.... To ensure a random sequence of numbers, you should call Randomize at least once (and preferrably only once) some time after opening the Excel file.

    One way is to create a Workbook_Open event macro. In VBA, press ctrl+R to open the Project Explorer pane on the left, double-click on ThisWorkbook, then copy the following and paste into VBA:
    Please Login or Register  to view this content.
    Alternatively, change your VBA function as follows:
    Please Login or Register  to view this content.
    Technically, the two are not the same. If VBA is reset (there are several ways that happens), Randomize will be called again. But that should be infrequently and "randomly" enough that it should not adversely affect the sequence of random numbers.
    I'm not certain what this does exactly, but now my StaticRand function does not work; it will not regenerate any random numbers anymore. The same numbers or question are always displayed even when I close and reopen Excel now. I removed the changes, and reverted back the way I originally had it, and it is still not regenerating the questions. It's like the function just died!

    I even removed the function and the module it was in. Saved and reopened the workbook, and the same questions are still there using the same numbers. With the function removed, there should be an error in place of the questions, but there is not.

    What could possible be going on? I don't even no where to begin to resolve this other than to create a new workbook and start over.

    Any suggestions?
    Mike

  12. #12
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: How do you restart/reset a Function?

    Quote Originally Posted by Modify_inc View Post
    I removed the changes, and reverted back the way I originally had it, and it is still not regenerating the questions. It's like the function just died!
    Obviously, you introduced a typo when you made the changes that I suggested. But I cannot help you further since you did not provide an exact copy of the code after you changed it. By "exact copy", I mean preferrably the URL of a downloadable copy of the Excel file. Alternatively, at a minimum, copy from VBA and paste into your posting, as you did in your original posting.

  13. #13
    Forum Contributor
    Join Date
    05-16-2007
    Location
    USA
    MS-Off Ver
    MS Office 2016, Excel 2016
    Posts
    214

    Re: How do you restart/reset a Function?

    Quote Originally Posted by joeu2004 View Post
    Obviously, you introduced a typo when you made the changes that I suggested. But I cannot help you further since you did not provide an exact copy of the code after you changed it. By "exact copy", I mean preferrably the URL of a downloadable copy of the Excel file. Alternatively, at a minimum, copy from VBA and paste into your posting, as you did in your original posting.
    Typo would infer I typed something, but I did not. I only copied and pasted your code. That is why I tried your suggestion before I tried MrShorty's, since yours only required a simple copy and paste. Of course I could have pasted them incorrectly though, I will give you that.

    The cells that use the StaticRand function will randomize correctly when I select them individually and press F2+enter on each of them. But that seems to be the only way as even closing the workbook and reopening it will continue to use the last numbers that were previously saved. I think this also confirms there is not an typo in the function if the function still works correctly when you press F2+enter on the relative cells in question. But even using my original code (without yours) it still behaves in the same fashion, so I am at a lost to what is going on.

    To be fair, I do not think your code resulted in this odd behavior, but I am still at a loss to why this is happening. Why does F2+enter still work, and why would Excel not execute the function after reopening the workbook? I've noticed that if I completely remove the function from VBA, use F2+enter on the cells (which now produces an error as they should since the function is missing), save the workbook, reopen it and open VBA, add the StaticRand function back, and save and close the workbook. When I reopen the workbook, I have new numbers/questions again. BUT if I save it at this point, close and reopen, I have the same numbers/questions all over again...The function stops working again, forcing me to use the F2+enter on each question.

    Thanks
    Mike

  14. #14
    Forum Contributor
    Join Date
    05-16-2007
    Location
    USA
    MS-Off Ver
    MS Office 2016, Excel 2016
    Posts
    214

    Re: How do you restart/reset a Function?

    I just added completely new unrelated code to perform another action using VBA, and it appears to not be working either. The code is suppose to protect a specific cell from being changed by the user. Yet it appears to not work as described, because I can still click on the cell and make changes to it. I'm not sure if it's something I'm doing wrong or if its an issue with Excel misbehaving.

    Here is the code:

    Please Login or Register  to view this content.
    Thank you
    Mike

+ 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. [SOLVED] On Error Restart Application and continue function
    By MarBoTJ in forum Access Tables & Databases
    Replies: 2
    Last Post: 10-23-2014, 02:13 PM
  2. reset function calls
    By kbka in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-17-2014, 09:00 AM
  3. How to reset normal function
    By ossa in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-30-2012, 02:18 AM
  4. Reset Range in Function
    By jaslake in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-01-2009, 11:20 PM
  5. [SOLVED] Is There a Function To Reset Macros?
    By JK in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-01-2005, 11:06 PM

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