Why is Excel re-executing functions in the same and different workbook when I create a new UDF and assign it...












2















I have two workbooks open.
In workbook A I have some UDFs assigned to cells and in workbook B I have some UDFs assigned to cells. These have already been executed, so the cells have values.
In workbook B (i.e. in the VBE) I create a new UDF, call it bar:



Function bar()
bar = "bar"
End Function


When I then go into Excel and assign bar() to a cell in workbook B (with =bar()) and then hit Enter, the UDFs in both the same workbook and in the other open workbook (workbook A) are triggered. They are re-executed, despite already having been executed and the cells having been populated with values.



Why is this? It's bizarre and very annoying behaviour even in my tiny little practice workbooks. What if I had 100 UDFs in a workbook? As soon as I add a new UDF and assign it to a cell, all the UDFs would be triggered.



I am using Excel 2016. Thanks in advance.










share|improve this question




















  • 2





    Excel recalculates all cells any time you change something. It's making sure that your change didn't affect any other cells. If you don't want it to happen, you can switch to manual calculation. I suspect someone will suggest changing the Volatile setting for these functions so they only calculate once, but it's not a good idea. Maybe you should use a Sub to populate these cells once, instead of Functions, which are meant to be dynamic.

    – ashleedawg
    Nov 23 '18 at 0:03













  • That was quick, thank you. But how do I make a UDF calculated manually, and does that mean the UDF won't be calculated when I hit enter after assigning to a cell the first time?

    – JL_SO
    Nov 23 '18 at 0:07






  • 2





    Calculation is basically either Automatic or Manual for the entire workbook, so this will not just include UDF's. Why is it a problem to calculate everything repeatedly? This is normal behavior and not a problem for properly written functions. With the example you gave, you could put that function in a million cells and you wouldn't even notice it recalculating anytime a cell was changed. See this and this for more info.

    – ashleedawg
    Nov 23 '18 at 0:10













  • It's a problem because the UDF is prompting me for input. This is just me learning VBA, and as per my book I've just been creating trivial functions, some of which take user input. So when I create a new UDF, I am getting prompted for user input again. I suppose in real life you'd probably not prompt for user input in a function; maybe it would be a button; I don't know. But as it is I have these UDFs that prompt for input and they keep getting retriggered when I add a function. Perhaps if I weren't developing the spreadsheet it wouldn't be an issue.

    – JL_SO
    Nov 23 '18 at 0:14






  • 1





    Then perhaps consider using helper cells for your input. I strongly dislike the idea of placing inputboxes inside of your UDFs - basically defeats the purpose of it being a UDF. If you don't want to use helper cells, then @ashleedawg's suggestion of changing them to a Sub is probably the route to go.

    – K.Dᴀᴠɪs
    Nov 23 '18 at 0:21


















2















I have two workbooks open.
In workbook A I have some UDFs assigned to cells and in workbook B I have some UDFs assigned to cells. These have already been executed, so the cells have values.
In workbook B (i.e. in the VBE) I create a new UDF, call it bar:



Function bar()
bar = "bar"
End Function


When I then go into Excel and assign bar() to a cell in workbook B (with =bar()) and then hit Enter, the UDFs in both the same workbook and in the other open workbook (workbook A) are triggered. They are re-executed, despite already having been executed and the cells having been populated with values.



Why is this? It's bizarre and very annoying behaviour even in my tiny little practice workbooks. What if I had 100 UDFs in a workbook? As soon as I add a new UDF and assign it to a cell, all the UDFs would be triggered.



I am using Excel 2016. Thanks in advance.










share|improve this question




















  • 2





    Excel recalculates all cells any time you change something. It's making sure that your change didn't affect any other cells. If you don't want it to happen, you can switch to manual calculation. I suspect someone will suggest changing the Volatile setting for these functions so they only calculate once, but it's not a good idea. Maybe you should use a Sub to populate these cells once, instead of Functions, which are meant to be dynamic.

    – ashleedawg
    Nov 23 '18 at 0:03













  • That was quick, thank you. But how do I make a UDF calculated manually, and does that mean the UDF won't be calculated when I hit enter after assigning to a cell the first time?

    – JL_SO
    Nov 23 '18 at 0:07






  • 2





    Calculation is basically either Automatic or Manual for the entire workbook, so this will not just include UDF's. Why is it a problem to calculate everything repeatedly? This is normal behavior and not a problem for properly written functions. With the example you gave, you could put that function in a million cells and you wouldn't even notice it recalculating anytime a cell was changed. See this and this for more info.

    – ashleedawg
    Nov 23 '18 at 0:10













  • It's a problem because the UDF is prompting me for input. This is just me learning VBA, and as per my book I've just been creating trivial functions, some of which take user input. So when I create a new UDF, I am getting prompted for user input again. I suppose in real life you'd probably not prompt for user input in a function; maybe it would be a button; I don't know. But as it is I have these UDFs that prompt for input and they keep getting retriggered when I add a function. Perhaps if I weren't developing the spreadsheet it wouldn't be an issue.

    – JL_SO
    Nov 23 '18 at 0:14






  • 1





    Then perhaps consider using helper cells for your input. I strongly dislike the idea of placing inputboxes inside of your UDFs - basically defeats the purpose of it being a UDF. If you don't want to use helper cells, then @ashleedawg's suggestion of changing them to a Sub is probably the route to go.

    – K.Dᴀᴠɪs
    Nov 23 '18 at 0:21
















2












2








2








I have two workbooks open.
In workbook A I have some UDFs assigned to cells and in workbook B I have some UDFs assigned to cells. These have already been executed, so the cells have values.
In workbook B (i.e. in the VBE) I create a new UDF, call it bar:



Function bar()
bar = "bar"
End Function


When I then go into Excel and assign bar() to a cell in workbook B (with =bar()) and then hit Enter, the UDFs in both the same workbook and in the other open workbook (workbook A) are triggered. They are re-executed, despite already having been executed and the cells having been populated with values.



Why is this? It's bizarre and very annoying behaviour even in my tiny little practice workbooks. What if I had 100 UDFs in a workbook? As soon as I add a new UDF and assign it to a cell, all the UDFs would be triggered.



I am using Excel 2016. Thanks in advance.










share|improve this question
















I have two workbooks open.
In workbook A I have some UDFs assigned to cells and in workbook B I have some UDFs assigned to cells. These have already been executed, so the cells have values.
In workbook B (i.e. in the VBE) I create a new UDF, call it bar:



Function bar()
bar = "bar"
End Function


When I then go into Excel and assign bar() to a cell in workbook B (with =bar()) and then hit Enter, the UDFs in both the same workbook and in the other open workbook (workbook A) are triggered. They are re-executed, despite already having been executed and the cells having been populated with values.



Why is this? It's bizarre and very annoying behaviour even in my tiny little practice workbooks. What if I had 100 UDFs in a workbook? As soon as I add a new UDF and assign it to a cell, all the UDFs would be triggered.



I am using Excel 2016. Thanks in advance.







excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 0:03







JL_SO

















asked Nov 23 '18 at 0:00









JL_SOJL_SO

343110




343110








  • 2





    Excel recalculates all cells any time you change something. It's making sure that your change didn't affect any other cells. If you don't want it to happen, you can switch to manual calculation. I suspect someone will suggest changing the Volatile setting for these functions so they only calculate once, but it's not a good idea. Maybe you should use a Sub to populate these cells once, instead of Functions, which are meant to be dynamic.

    – ashleedawg
    Nov 23 '18 at 0:03













  • That was quick, thank you. But how do I make a UDF calculated manually, and does that mean the UDF won't be calculated when I hit enter after assigning to a cell the first time?

    – JL_SO
    Nov 23 '18 at 0:07






  • 2





    Calculation is basically either Automatic or Manual for the entire workbook, so this will not just include UDF's. Why is it a problem to calculate everything repeatedly? This is normal behavior and not a problem for properly written functions. With the example you gave, you could put that function in a million cells and you wouldn't even notice it recalculating anytime a cell was changed. See this and this for more info.

    – ashleedawg
    Nov 23 '18 at 0:10













  • It's a problem because the UDF is prompting me for input. This is just me learning VBA, and as per my book I've just been creating trivial functions, some of which take user input. So when I create a new UDF, I am getting prompted for user input again. I suppose in real life you'd probably not prompt for user input in a function; maybe it would be a button; I don't know. But as it is I have these UDFs that prompt for input and they keep getting retriggered when I add a function. Perhaps if I weren't developing the spreadsheet it wouldn't be an issue.

    – JL_SO
    Nov 23 '18 at 0:14






  • 1





    Then perhaps consider using helper cells for your input. I strongly dislike the idea of placing inputboxes inside of your UDFs - basically defeats the purpose of it being a UDF. If you don't want to use helper cells, then @ashleedawg's suggestion of changing them to a Sub is probably the route to go.

    – K.Dᴀᴠɪs
    Nov 23 '18 at 0:21
















  • 2





    Excel recalculates all cells any time you change something. It's making sure that your change didn't affect any other cells. If you don't want it to happen, you can switch to manual calculation. I suspect someone will suggest changing the Volatile setting for these functions so they only calculate once, but it's not a good idea. Maybe you should use a Sub to populate these cells once, instead of Functions, which are meant to be dynamic.

    – ashleedawg
    Nov 23 '18 at 0:03













  • That was quick, thank you. But how do I make a UDF calculated manually, and does that mean the UDF won't be calculated when I hit enter after assigning to a cell the first time?

    – JL_SO
    Nov 23 '18 at 0:07






  • 2





    Calculation is basically either Automatic or Manual for the entire workbook, so this will not just include UDF's. Why is it a problem to calculate everything repeatedly? This is normal behavior and not a problem for properly written functions. With the example you gave, you could put that function in a million cells and you wouldn't even notice it recalculating anytime a cell was changed. See this and this for more info.

    – ashleedawg
    Nov 23 '18 at 0:10













  • It's a problem because the UDF is prompting me for input. This is just me learning VBA, and as per my book I've just been creating trivial functions, some of which take user input. So when I create a new UDF, I am getting prompted for user input again. I suppose in real life you'd probably not prompt for user input in a function; maybe it would be a button; I don't know. But as it is I have these UDFs that prompt for input and they keep getting retriggered when I add a function. Perhaps if I weren't developing the spreadsheet it wouldn't be an issue.

    – JL_SO
    Nov 23 '18 at 0:14






  • 1





    Then perhaps consider using helper cells for your input. I strongly dislike the idea of placing inputboxes inside of your UDFs - basically defeats the purpose of it being a UDF. If you don't want to use helper cells, then @ashleedawg's suggestion of changing them to a Sub is probably the route to go.

    – K.Dᴀᴠɪs
    Nov 23 '18 at 0:21










2




2





Excel recalculates all cells any time you change something. It's making sure that your change didn't affect any other cells. If you don't want it to happen, you can switch to manual calculation. I suspect someone will suggest changing the Volatile setting for these functions so they only calculate once, but it's not a good idea. Maybe you should use a Sub to populate these cells once, instead of Functions, which are meant to be dynamic.

– ashleedawg
Nov 23 '18 at 0:03







Excel recalculates all cells any time you change something. It's making sure that your change didn't affect any other cells. If you don't want it to happen, you can switch to manual calculation. I suspect someone will suggest changing the Volatile setting for these functions so they only calculate once, but it's not a good idea. Maybe you should use a Sub to populate these cells once, instead of Functions, which are meant to be dynamic.

– ashleedawg
Nov 23 '18 at 0:03















That was quick, thank you. But how do I make a UDF calculated manually, and does that mean the UDF won't be calculated when I hit enter after assigning to a cell the first time?

– JL_SO
Nov 23 '18 at 0:07





That was quick, thank you. But how do I make a UDF calculated manually, and does that mean the UDF won't be calculated when I hit enter after assigning to a cell the first time?

– JL_SO
Nov 23 '18 at 0:07




2




2





Calculation is basically either Automatic or Manual for the entire workbook, so this will not just include UDF's. Why is it a problem to calculate everything repeatedly? This is normal behavior and not a problem for properly written functions. With the example you gave, you could put that function in a million cells and you wouldn't even notice it recalculating anytime a cell was changed. See this and this for more info.

– ashleedawg
Nov 23 '18 at 0:10







Calculation is basically either Automatic or Manual for the entire workbook, so this will not just include UDF's. Why is it a problem to calculate everything repeatedly? This is normal behavior and not a problem for properly written functions. With the example you gave, you could put that function in a million cells and you wouldn't even notice it recalculating anytime a cell was changed. See this and this for more info.

– ashleedawg
Nov 23 '18 at 0:10















It's a problem because the UDF is prompting me for input. This is just me learning VBA, and as per my book I've just been creating trivial functions, some of which take user input. So when I create a new UDF, I am getting prompted for user input again. I suppose in real life you'd probably not prompt for user input in a function; maybe it would be a button; I don't know. But as it is I have these UDFs that prompt for input and they keep getting retriggered when I add a function. Perhaps if I weren't developing the spreadsheet it wouldn't be an issue.

– JL_SO
Nov 23 '18 at 0:14





It's a problem because the UDF is prompting me for input. This is just me learning VBA, and as per my book I've just been creating trivial functions, some of which take user input. So when I create a new UDF, I am getting prompted for user input again. I suppose in real life you'd probably not prompt for user input in a function; maybe it would be a button; I don't know. But as it is I have these UDFs that prompt for input and they keep getting retriggered when I add a function. Perhaps if I weren't developing the spreadsheet it wouldn't be an issue.

– JL_SO
Nov 23 '18 at 0:14




1




1





Then perhaps consider using helper cells for your input. I strongly dislike the idea of placing inputboxes inside of your UDFs - basically defeats the purpose of it being a UDF. If you don't want to use helper cells, then @ashleedawg's suggestion of changing them to a Sub is probably the route to go.

– K.Dᴀᴠɪs
Nov 23 '18 at 0:21







Then perhaps consider using helper cells for your input. I strongly dislike the idea of placing inputboxes inside of your UDFs - basically defeats the purpose of it being a UDF. If you don't want to use helper cells, then @ashleedawg's suggestion of changing them to a Sub is probably the route to go.

– K.Dᴀᴠɪs
Nov 23 '18 at 0:21














1 Answer
1






active

oldest

votes


















2














So your issue is that you are placing InputBox() functions inside your UDF. While you can certainly do this, it essentially defeats the purpose of using a UDF to begin with.



Consider placing an argument inside your UDF that pulls the data from the worksheet instead of you prompting for the user's input every time recalc occurs. Or make a global variable that will remain active while the worksheet is open.



Using UDF Arguments



If you had a UDF that looked like this:



Public Function myUDF()
Dim myInput As String
myInput = InputBox("Type Something")
myUDF = myInput & "!"
End Function


You could add an argument that will pull your myInput from the worksheet instead:



Public Function myUDF(myInput As String) As String
myUDF = myInput & "!"
End Function


Which on your worksheet, would look like this:



=myUDF(A1)  'Where A1 is your new `myInput` argument


Using Global Variables



You can also use a global variable that will get your input only when you want to change it. Perhaps you can create a command button that you will press to prompt for your input, or if you only need to prompt it one time - you could use the Workbook_Open() event to set this variable - or a combination of both.



So at the top of a standard module, you could add your global variable:



Public glbVarStr As String


And then you would just run any Sub to set your global variable:



' Since this is a WORKBOOK event, you would need to place this sub in
' your workbook's code module
Private Sub Workbook_Open()
glbVarStr = Inputbox("Type Something")
End Sub


which you would then add to your UDF:



Public Function myUDF() As String
myUDF = glbVarStr & "!"
End Function





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439249%2fwhy-is-excel-re-executing-functions-in-the-same-and-different-workbook-when-i-cr%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    So your issue is that you are placing InputBox() functions inside your UDF. While you can certainly do this, it essentially defeats the purpose of using a UDF to begin with.



    Consider placing an argument inside your UDF that pulls the data from the worksheet instead of you prompting for the user's input every time recalc occurs. Or make a global variable that will remain active while the worksheet is open.



    Using UDF Arguments



    If you had a UDF that looked like this:



    Public Function myUDF()
    Dim myInput As String
    myInput = InputBox("Type Something")
    myUDF = myInput & "!"
    End Function


    You could add an argument that will pull your myInput from the worksheet instead:



    Public Function myUDF(myInput As String) As String
    myUDF = myInput & "!"
    End Function


    Which on your worksheet, would look like this:



    =myUDF(A1)  'Where A1 is your new `myInput` argument


    Using Global Variables



    You can also use a global variable that will get your input only when you want to change it. Perhaps you can create a command button that you will press to prompt for your input, or if you only need to prompt it one time - you could use the Workbook_Open() event to set this variable - or a combination of both.



    So at the top of a standard module, you could add your global variable:



    Public glbVarStr As String


    And then you would just run any Sub to set your global variable:



    ' Since this is a WORKBOOK event, you would need to place this sub in
    ' your workbook's code module
    Private Sub Workbook_Open()
    glbVarStr = Inputbox("Type Something")
    End Sub


    which you would then add to your UDF:



    Public Function myUDF() As String
    myUDF = glbVarStr & "!"
    End Function





    share|improve this answer






























      2














      So your issue is that you are placing InputBox() functions inside your UDF. While you can certainly do this, it essentially defeats the purpose of using a UDF to begin with.



      Consider placing an argument inside your UDF that pulls the data from the worksheet instead of you prompting for the user's input every time recalc occurs. Or make a global variable that will remain active while the worksheet is open.



      Using UDF Arguments



      If you had a UDF that looked like this:



      Public Function myUDF()
      Dim myInput As String
      myInput = InputBox("Type Something")
      myUDF = myInput & "!"
      End Function


      You could add an argument that will pull your myInput from the worksheet instead:



      Public Function myUDF(myInput As String) As String
      myUDF = myInput & "!"
      End Function


      Which on your worksheet, would look like this:



      =myUDF(A1)  'Where A1 is your new `myInput` argument


      Using Global Variables



      You can also use a global variable that will get your input only when you want to change it. Perhaps you can create a command button that you will press to prompt for your input, or if you only need to prompt it one time - you could use the Workbook_Open() event to set this variable - or a combination of both.



      So at the top of a standard module, you could add your global variable:



      Public glbVarStr As String


      And then you would just run any Sub to set your global variable:



      ' Since this is a WORKBOOK event, you would need to place this sub in
      ' your workbook's code module
      Private Sub Workbook_Open()
      glbVarStr = Inputbox("Type Something")
      End Sub


      which you would then add to your UDF:



      Public Function myUDF() As String
      myUDF = glbVarStr & "!"
      End Function





      share|improve this answer




























        2












        2








        2







        So your issue is that you are placing InputBox() functions inside your UDF. While you can certainly do this, it essentially defeats the purpose of using a UDF to begin with.



        Consider placing an argument inside your UDF that pulls the data from the worksheet instead of you prompting for the user's input every time recalc occurs. Or make a global variable that will remain active while the worksheet is open.



        Using UDF Arguments



        If you had a UDF that looked like this:



        Public Function myUDF()
        Dim myInput As String
        myInput = InputBox("Type Something")
        myUDF = myInput & "!"
        End Function


        You could add an argument that will pull your myInput from the worksheet instead:



        Public Function myUDF(myInput As String) As String
        myUDF = myInput & "!"
        End Function


        Which on your worksheet, would look like this:



        =myUDF(A1)  'Where A1 is your new `myInput` argument


        Using Global Variables



        You can also use a global variable that will get your input only when you want to change it. Perhaps you can create a command button that you will press to prompt for your input, or if you only need to prompt it one time - you could use the Workbook_Open() event to set this variable - or a combination of both.



        So at the top of a standard module, you could add your global variable:



        Public glbVarStr As String


        And then you would just run any Sub to set your global variable:



        ' Since this is a WORKBOOK event, you would need to place this sub in
        ' your workbook's code module
        Private Sub Workbook_Open()
        glbVarStr = Inputbox("Type Something")
        End Sub


        which you would then add to your UDF:



        Public Function myUDF() As String
        myUDF = glbVarStr & "!"
        End Function





        share|improve this answer















        So your issue is that you are placing InputBox() functions inside your UDF. While you can certainly do this, it essentially defeats the purpose of using a UDF to begin with.



        Consider placing an argument inside your UDF that pulls the data from the worksheet instead of you prompting for the user's input every time recalc occurs. Or make a global variable that will remain active while the worksheet is open.



        Using UDF Arguments



        If you had a UDF that looked like this:



        Public Function myUDF()
        Dim myInput As String
        myInput = InputBox("Type Something")
        myUDF = myInput & "!"
        End Function


        You could add an argument that will pull your myInput from the worksheet instead:



        Public Function myUDF(myInput As String) As String
        myUDF = myInput & "!"
        End Function


        Which on your worksheet, would look like this:



        =myUDF(A1)  'Where A1 is your new `myInput` argument


        Using Global Variables



        You can also use a global variable that will get your input only when you want to change it. Perhaps you can create a command button that you will press to prompt for your input, or if you only need to prompt it one time - you could use the Workbook_Open() event to set this variable - or a combination of both.



        So at the top of a standard module, you could add your global variable:



        Public glbVarStr As String


        And then you would just run any Sub to set your global variable:



        ' Since this is a WORKBOOK event, you would need to place this sub in
        ' your workbook's code module
        Private Sub Workbook_Open()
        glbVarStr = Inputbox("Type Something")
        End Sub


        which you would then add to your UDF:



        Public Function myUDF() As String
        myUDF = glbVarStr & "!"
        End Function






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 0:41

























        answered Nov 23 '18 at 0:35









        K.DᴀᴠɪsK.Dᴀᴠɪs

        7,310112440




        7,310112440
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439249%2fwhy-is-excel-re-executing-functions-in-the-same-and-different-workbook-when-i-cr%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy