+ Reply to Thread
Results 1 to 5 of 5

Any way to use VBA to comment out / uncomment a standard module?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-27-2012
    Location
    Uk
    MS-Off Ver
    Excel 2010
    Posts
    98

    Question Any way to use VBA to comment out / uncomment a standard module?

    Hi All

    I have this module in several workbooks

    Module Name: Has_Formula
    Function HasFormula(rCell As Range) As Boolean
        Application.Volatile
        HasFormula = rCell.HasFormula
    End Function
    I'm hoping there's a way to use the Workbook Open() event to un-comment all 4 lines
    And Workbook_BeforeClose() event to comment them all back out again

    Is that even possible?

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Any way to use VBA to comment out / uncomment a standard module?

    Rather than commenting out a UDF, its much easier just to not use it.

    And, yes you could use code in the Open event to remove all the REMs from commented out code. The problem is that you couldn't use that module until the changes were Saved.
    I should mention, that if you are working in a company environment, your Permissions might be set so that you can't edit vb code on the fly.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Any way to use VBA to comment out / uncomment a standard module?

    Hi again,

    Take a look at the attached workbook and see if it does what you need. It uses the following code:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub CommentOutRoutine()
    
        Call RoutineIsCommentedOut(bTrueOrFalse:=True)
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub RestoreRoutine()
    
        Call RoutineIsCommentedOut(bTrueOrFalse:=False)
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub RoutineIsCommentedOut(bTrueOrFalse As Boolean)
    
        Const sMODULE_NAME  As String = "Has_Formula"
        Const sCOMMENT      As String = "'   "
        Const sLINE_1       As String = "Function HasFormula(rCell As Range) As Boolean"
        Const sLINE_2       As String = "    Application.Volatile"
        Const sLINE_3       As String = "    HasFormula = rCell.HasFormula"
        Const sLINE_4       As String = "End Function"
    
        Dim codHas_Formula  As Object
        Dim sNewLine_1      As String
        Dim sNewLine_2      As String
        Dim sNewLine_3      As String
        Dim sNewLine_4      As String
        Dim iLineNo         As Long
        Dim wbk             As Workbook
        Dim vbp             As Object
    
        Set wbk = ActiveWorkbook
        Set vbp = wbk.VBProject
    
        Set codHas_Formula = vbp.VBComponents(sMODULE_NAME).CodeModule
    
        For iLineNo = 1 To codHas_Formula.CountOfLines
    
            If codHas_Formula.Find(Target:=sLINE_1, _
                                   StartLine:=iLineNo, StartColumn:=1, _
                                   EndLine:=iLineNo, EndColumn:=999, _
                                   WholeWord:=True) = True Then
    
                If bTrueOrFalse = True Then
    
                      sNewLine_1 = sCOMMENT & sLINE_1
                      sNewLine_2 = sCOMMENT & sLINE_2
                      sNewLine_3 = sCOMMENT & sLINE_3
                      sNewLine_4 = sCOMMENT & sLINE_4
    
                Else: sNewLine_1 = sLINE_1
                      sNewLine_2 = sLINE_2
                      sNewLine_3 = sLINE_3
                      sNewLine_4 = sLINE_4
    
                End If
    
                codHas_Formula.ReplaceLine Line:=iLineNo + 0, String:=sNewLine_1
                codHas_Formula.ReplaceLine Line:=iLineNo + 1, String:=sNewLine_2
                codHas_Formula.ReplaceLine Line:=iLineNo + 2, String:=sNewLine_3
                codHas_Formula.ReplaceLine Line:=iLineNo + 3, String:=sNewLine_4
    
                Exit For
    
            End If
    
        Next iLineNo
    
    End Sub

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M



    P. S. The above approach uses late binding, with the variables "codHas_Formula" and "vbp" being declared as type Object - you can use early binding instead if you create a reference to "Microsoft Visual Basic for Applications Extensibility 5.3"
    Attached Files Attached Files
    Last edited by Greg M; 09-04-2020 at 08:30 PM. Reason: P. S. added

  4. #4
    Registered User
    Join Date
    10-27-2012
    Location
    Uk
    MS-Off Ver
    Excel 2010
    Posts
    98

    Re: Any way to use VBA to comment out / uncomment a standard module?

    Ha! That is excellent! Thank you Greg
    I've saved that workbook in my bag of tricks for future

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Any way to use VBA to comment out / uncomment a standard module?

    Hi again,

    Many thanks for your very prompt feedback and also for the Reputation increase - much appreciated!

    You're welcome - glad I was able to help.

    Best regards,

    Greg M

+ 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] Can/should you put Worksheet code into a standard module?
    By aquixano in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-27-2017, 09:27 PM
  2. What change should I do to run this macro from standard module?
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-12-2014, 12:34 AM
  3. What can a class do that can't be duplicated in a standard module?
    By foxguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-14-2014, 09:20 AM
  4. [SOLVED] Code locks cells when inserted in sheet module but returns error in standard module
    By yoda66 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2014, 07:39 AM
  5. VB Code to comment/uncomment code
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-25-2013, 09:06 AM
  6. [SOLVED] Use Target in standard module
    By tone640 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-23-2012, 08:18 AM
  7. Is this a standard module?
    By marianmix in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-27-2011, 03:24 PM

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