How to identify broken vba code in absence of compile/run-time errors?












1















I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?



For example:



Let's say there is a variable defined in ModuleA.bas as:



Public Const REG_SZ AS Long = 1



Other files, say ModuleB.bas, ModuleC.bas, etc make use of this variable.



Now, if we delete the ModuleA.bas file that contains the variable REG_SZ, and build the project, then we see no compile or run-time errors. Only while debugging you realize that the program defaults this variable to be having no value. When you right click this variable in the other modules and click on 'Definiton' you get an error that says: "Identifier under cursor is not recognized". The solution is to declare this variable locally on each file, or re-instate the original ModuleA.bas file with that variable declared.



I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?










share|improve this question




















  • 1





    You might want to take a look at the Rubberduck add-in. It will inspect your code for common problems (including undeclared variables). Full disclosure, I am a contributor.

    – Comintern
    Nov 22 '18 at 13:54
















1















I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?



For example:



Let's say there is a variable defined in ModuleA.bas as:



Public Const REG_SZ AS Long = 1



Other files, say ModuleB.bas, ModuleC.bas, etc make use of this variable.



Now, if we delete the ModuleA.bas file that contains the variable REG_SZ, and build the project, then we see no compile or run-time errors. Only while debugging you realize that the program defaults this variable to be having no value. When you right click this variable in the other modules and click on 'Definiton' you get an error that says: "Identifier under cursor is not recognized". The solution is to declare this variable locally on each file, or re-instate the original ModuleA.bas file with that variable declared.



I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?










share|improve this question




















  • 1





    You might want to take a look at the Rubberduck add-in. It will inspect your code for common problems (including undeclared variables). Full disclosure, I am a contributor.

    – Comintern
    Nov 22 '18 at 13:54














1












1








1








I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?



For example:



Let's say there is a variable defined in ModuleA.bas as:



Public Const REG_SZ AS Long = 1



Other files, say ModuleB.bas, ModuleC.bas, etc make use of this variable.



Now, if we delete the ModuleA.bas file that contains the variable REG_SZ, and build the project, then we see no compile or run-time errors. Only while debugging you realize that the program defaults this variable to be having no value. When you right click this variable in the other modules and click on 'Definiton' you get an error that says: "Identifier under cursor is not recognized". The solution is to declare this variable locally on each file, or re-instate the original ModuleA.bas file with that variable declared.



I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?










share|improve this question
















I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?



For example:



Let's say there is a variable defined in ModuleA.bas as:



Public Const REG_SZ AS Long = 1



Other files, say ModuleB.bas, ModuleC.bas, etc make use of this variable.



Now, if we delete the ModuleA.bas file that contains the variable REG_SZ, and build the project, then we see no compile or run-time errors. Only while debugging you realize that the program defaults this variable to be having no value. When you right click this variable in the other modules and click on 'Definiton' you get an error that says: "Identifier under cursor is not recognized". The solution is to declare this variable locally on each file, or re-instate the original ModuleA.bas file with that variable declared.



I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?







vba ms-word






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 11:53







variable

















asked Nov 22 '18 at 10:51









variablevariable

96321840




96321840








  • 1





    You might want to take a look at the Rubberduck add-in. It will inspect your code for common problems (including undeclared variables). Full disclosure, I am a contributor.

    – Comintern
    Nov 22 '18 at 13:54














  • 1





    You might want to take a look at the Rubberduck add-in. It will inspect your code for common problems (including undeclared variables). Full disclosure, I am a contributor.

    – Comintern
    Nov 22 '18 at 13:54








1




1





You might want to take a look at the Rubberduck add-in. It will inspect your code for common problems (including undeclared variables). Full disclosure, I am a contributor.

– Comintern
Nov 22 '18 at 13:54





You might want to take a look at the Rubberduck add-in. It will inspect your code for common problems (including undeclared variables). Full disclosure, I am a contributor.

– Comintern
Nov 22 '18 at 13:54












2 Answers
2






active

oldest

votes


















2














To answer your specific question on variables ... Put an Option Explicit statement at the top of each code module. Any VBA code you insert that does not declare its variables will automatically be flagged without having to compile the the code.






share|improve this answer


























  • Do you mean it will force variable declaration on that file itself? Or is it OK as long as a variable is available somewhere in the project.?

    – variable
    Nov 22 '18 at 11:59











  • @variable It's specific to the code module. If you have multiple modules, each must have an Option Explicit. Each UserForm would also need the statement.

    – Rich Michaels
    Nov 22 '18 at 12:02













  • @variable it depends wheter it's a public or private variable. Try searching/reading about it because it's a bit complex.

    – Patrick Honorez
    Nov 22 '18 at 12:03











  • Any article links or search keywords on this concept will be appreciated please. thank you.

    – variable
    Nov 22 '18 at 12:07






  • 2





    @variable There also is a setting in the VBE's options menu: Require variable declaration (or similar) - this will automatically add Option Explicit to any new module created from that point on. (I highly recommend activating that setting!)

    – Inarion
    Nov 22 '18 at 14:27



















2














There's a setting in the [Editor] tab of the VBE options dialog labelled "Require Variable Declaration", that will automatically add Option Explicit to every module:



require variable declaration



Having Option Explicit in a module's declarations section makes VBA refuse to compile/run a project that uses undeclared variables.



The Visual Basic Editor (VBE) will not help much beyond that, however VBE add-ins, such as Rubberduck, can push static code analysis much further - for example it can warn you when Option Explicit is missing from a module, if a variable is never assigned or if a procedure isn't used, if an assignment is redundant or overwritten before its value is used, if the IsMissing function is misused, ...and so many others (approximately 80 inspections are implemented as of v2.3, scheduled to release November 25):



Rubberduck code inspections toolwindow



Disclaimer: I manage this open-source project.






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%2f53429300%2fhow-to-identify-broken-vba-code-in-absence-of-compile-run-time-errors%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    To answer your specific question on variables ... Put an Option Explicit statement at the top of each code module. Any VBA code you insert that does not declare its variables will automatically be flagged without having to compile the the code.






    share|improve this answer


























    • Do you mean it will force variable declaration on that file itself? Or is it OK as long as a variable is available somewhere in the project.?

      – variable
      Nov 22 '18 at 11:59











    • @variable It's specific to the code module. If you have multiple modules, each must have an Option Explicit. Each UserForm would also need the statement.

      – Rich Michaels
      Nov 22 '18 at 12:02













    • @variable it depends wheter it's a public or private variable. Try searching/reading about it because it's a bit complex.

      – Patrick Honorez
      Nov 22 '18 at 12:03











    • Any article links or search keywords on this concept will be appreciated please. thank you.

      – variable
      Nov 22 '18 at 12:07






    • 2





      @variable There also is a setting in the VBE's options menu: Require variable declaration (or similar) - this will automatically add Option Explicit to any new module created from that point on. (I highly recommend activating that setting!)

      – Inarion
      Nov 22 '18 at 14:27
















    2














    To answer your specific question on variables ... Put an Option Explicit statement at the top of each code module. Any VBA code you insert that does not declare its variables will automatically be flagged without having to compile the the code.






    share|improve this answer


























    • Do you mean it will force variable declaration on that file itself? Or is it OK as long as a variable is available somewhere in the project.?

      – variable
      Nov 22 '18 at 11:59











    • @variable It's specific to the code module. If you have multiple modules, each must have an Option Explicit. Each UserForm would also need the statement.

      – Rich Michaels
      Nov 22 '18 at 12:02













    • @variable it depends wheter it's a public or private variable. Try searching/reading about it because it's a bit complex.

      – Patrick Honorez
      Nov 22 '18 at 12:03











    • Any article links or search keywords on this concept will be appreciated please. thank you.

      – variable
      Nov 22 '18 at 12:07






    • 2





      @variable There also is a setting in the VBE's options menu: Require variable declaration (or similar) - this will automatically add Option Explicit to any new module created from that point on. (I highly recommend activating that setting!)

      – Inarion
      Nov 22 '18 at 14:27














    2












    2








    2







    To answer your specific question on variables ... Put an Option Explicit statement at the top of each code module. Any VBA code you insert that does not declare its variables will automatically be flagged without having to compile the the code.






    share|improve this answer















    To answer your specific question on variables ... Put an Option Explicit statement at the top of each code module. Any VBA code you insert that does not declare its variables will automatically be flagged without having to compile the the code.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 12:00

























    answered Nov 22 '18 at 11:58









    Rich MichaelsRich Michaels

    536248




    536248













    • Do you mean it will force variable declaration on that file itself? Or is it OK as long as a variable is available somewhere in the project.?

      – variable
      Nov 22 '18 at 11:59











    • @variable It's specific to the code module. If you have multiple modules, each must have an Option Explicit. Each UserForm would also need the statement.

      – Rich Michaels
      Nov 22 '18 at 12:02













    • @variable it depends wheter it's a public or private variable. Try searching/reading about it because it's a bit complex.

      – Patrick Honorez
      Nov 22 '18 at 12:03











    • Any article links or search keywords on this concept will be appreciated please. thank you.

      – variable
      Nov 22 '18 at 12:07






    • 2





      @variable There also is a setting in the VBE's options menu: Require variable declaration (or similar) - this will automatically add Option Explicit to any new module created from that point on. (I highly recommend activating that setting!)

      – Inarion
      Nov 22 '18 at 14:27



















    • Do you mean it will force variable declaration on that file itself? Or is it OK as long as a variable is available somewhere in the project.?

      – variable
      Nov 22 '18 at 11:59











    • @variable It's specific to the code module. If you have multiple modules, each must have an Option Explicit. Each UserForm would also need the statement.

      – Rich Michaels
      Nov 22 '18 at 12:02













    • @variable it depends wheter it's a public or private variable. Try searching/reading about it because it's a bit complex.

      – Patrick Honorez
      Nov 22 '18 at 12:03











    • Any article links or search keywords on this concept will be appreciated please. thank you.

      – variable
      Nov 22 '18 at 12:07






    • 2





      @variable There also is a setting in the VBE's options menu: Require variable declaration (or similar) - this will automatically add Option Explicit to any new module created from that point on. (I highly recommend activating that setting!)

      – Inarion
      Nov 22 '18 at 14:27

















    Do you mean it will force variable declaration on that file itself? Or is it OK as long as a variable is available somewhere in the project.?

    – variable
    Nov 22 '18 at 11:59





    Do you mean it will force variable declaration on that file itself? Or is it OK as long as a variable is available somewhere in the project.?

    – variable
    Nov 22 '18 at 11:59













    @variable It's specific to the code module. If you have multiple modules, each must have an Option Explicit. Each UserForm would also need the statement.

    – Rich Michaels
    Nov 22 '18 at 12:02







    @variable It's specific to the code module. If you have multiple modules, each must have an Option Explicit. Each UserForm would also need the statement.

    – Rich Michaels
    Nov 22 '18 at 12:02















    @variable it depends wheter it's a public or private variable. Try searching/reading about it because it's a bit complex.

    – Patrick Honorez
    Nov 22 '18 at 12:03





    @variable it depends wheter it's a public or private variable. Try searching/reading about it because it's a bit complex.

    – Patrick Honorez
    Nov 22 '18 at 12:03













    Any article links or search keywords on this concept will be appreciated please. thank you.

    – variable
    Nov 22 '18 at 12:07





    Any article links or search keywords on this concept will be appreciated please. thank you.

    – variable
    Nov 22 '18 at 12:07




    2




    2





    @variable There also is a setting in the VBE's options menu: Require variable declaration (or similar) - this will automatically add Option Explicit to any new module created from that point on. (I highly recommend activating that setting!)

    – Inarion
    Nov 22 '18 at 14:27





    @variable There also is a setting in the VBE's options menu: Require variable declaration (or similar) - this will automatically add Option Explicit to any new module created from that point on. (I highly recommend activating that setting!)

    – Inarion
    Nov 22 '18 at 14:27













    2














    There's a setting in the [Editor] tab of the VBE options dialog labelled "Require Variable Declaration", that will automatically add Option Explicit to every module:



    require variable declaration



    Having Option Explicit in a module's declarations section makes VBA refuse to compile/run a project that uses undeclared variables.



    The Visual Basic Editor (VBE) will not help much beyond that, however VBE add-ins, such as Rubberduck, can push static code analysis much further - for example it can warn you when Option Explicit is missing from a module, if a variable is never assigned or if a procedure isn't used, if an assignment is redundant or overwritten before its value is used, if the IsMissing function is misused, ...and so many others (approximately 80 inspections are implemented as of v2.3, scheduled to release November 25):



    Rubberduck code inspections toolwindow



    Disclaimer: I manage this open-source project.






    share|improve this answer




























      2














      There's a setting in the [Editor] tab of the VBE options dialog labelled "Require Variable Declaration", that will automatically add Option Explicit to every module:



      require variable declaration



      Having Option Explicit in a module's declarations section makes VBA refuse to compile/run a project that uses undeclared variables.



      The Visual Basic Editor (VBE) will not help much beyond that, however VBE add-ins, such as Rubberduck, can push static code analysis much further - for example it can warn you when Option Explicit is missing from a module, if a variable is never assigned or if a procedure isn't used, if an assignment is redundant or overwritten before its value is used, if the IsMissing function is misused, ...and so many others (approximately 80 inspections are implemented as of v2.3, scheduled to release November 25):



      Rubberduck code inspections toolwindow



      Disclaimer: I manage this open-source project.






      share|improve this answer


























        2












        2








        2







        There's a setting in the [Editor] tab of the VBE options dialog labelled "Require Variable Declaration", that will automatically add Option Explicit to every module:



        require variable declaration



        Having Option Explicit in a module's declarations section makes VBA refuse to compile/run a project that uses undeclared variables.



        The Visual Basic Editor (VBE) will not help much beyond that, however VBE add-ins, such as Rubberduck, can push static code analysis much further - for example it can warn you when Option Explicit is missing from a module, if a variable is never assigned or if a procedure isn't used, if an assignment is redundant or overwritten before its value is used, if the IsMissing function is misused, ...and so many others (approximately 80 inspections are implemented as of v2.3, scheduled to release November 25):



        Rubberduck code inspections toolwindow



        Disclaimer: I manage this open-source project.






        share|improve this answer













        There's a setting in the [Editor] tab of the VBE options dialog labelled "Require Variable Declaration", that will automatically add Option Explicit to every module:



        require variable declaration



        Having Option Explicit in a module's declarations section makes VBA refuse to compile/run a project that uses undeclared variables.



        The Visual Basic Editor (VBE) will not help much beyond that, however VBE add-ins, such as Rubberduck, can push static code analysis much further - for example it can warn you when Option Explicit is missing from a module, if a variable is never assigned or if a procedure isn't used, if an assignment is redundant or overwritten before its value is used, if the IsMissing function is misused, ...and so many others (approximately 80 inspections are implemented as of v2.3, scheduled to release November 25):



        Rubberduck code inspections toolwindow



        Disclaimer: I manage this open-source project.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 15:02









        Mathieu GuindonMathieu Guindon

        41.5k762143




        41.5k762143






























            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%2f53429300%2fhow-to-identify-broken-vba-code-in-absence-of-compile-run-time-errors%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'