Consulting

Results 1 to 5 of 5

Thread: Check for presence of VBA Procedure in all modules

  1. #1
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location

    Check for presence of VBA Procedure in all modules

    Good day,


    The following is code provided by Chip Pearson, my problem is how to apply it to determine if a Procedure called "TestProcedure" is present in one of the modules of the workbook:


    From Chip Pearson:

    Testing If A VBComponent Exists
    This code will return True or False indicating whether the VBComponent named by VBCompName exists in the project referenced by VBProj. If VBProj is omitted, the VBProject of the ActiveWorkbook is used.
    [vba]Public Function VBComponentExists(VBCompName As String, Optional VBProj As VBIDE.VBProject = Nothing) As Boolean
    ' This returns True or False indicating whether a VBComponent named VBCompName exists in the VBProject _
    referenced by VBProj. If VBProj is omitted, the VBProject of the ActiveWorkbook is used.
    Dim VBP As VBIDE.VBProject
    If VBProj Is Nothing Then
    Set VBP = ActiveWorkbook.VBProject
    Else Set VBP = VBProj
    End If
    On Error Resume Next
    VBComponentExists = CBool(Len(VBP.VBComponents(VBCompName).Name))
    End Function[/vba]


    I would appreciate any assistance.
    I have tried to use it but get "False" even though the procedure does in fact exist!

    Thank you

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    You'll need to read Chips site properly on how to call and use this function, Chips gone to great lengths to have a detailed well laid out explanation.

    (your copy and past put everything in to one line so most of the code was commented out, i've adjusted that for you)
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This OP code will test if a VBComponent is present.
    This will test if a procedure exists in some module of the indicated workbook.


    Function ProcedureExists(proceedureName, wb As Workbook) As Boolean
        Dim myComp As VBComponent
        Dim myModule As CodeModule
        Dim procType As Variant
        
        For Each myComp In wb.VBProject.VBComponents
            Set myModule = myComp.CodeModule
            On Error Resume Next
            For Each procType In Array(vbext_pk_Proc, vbext_pk_Get, vbext_pk_Let, vbext_pk_Set)
                ProcedureExists = (myModule.ProcStartLine(proceedureName, procType) <> 0) Or ProcedureExists
            Next procType
            On Error GoTo 0
        Next myComp
    End Function
    Last edited by mikerickson; 07-31-2013 at 07:40 AM.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    or
    [VBA]Function ProcedureExists(procName, wb As Workbook) As Boolean
    On Error Resume Next

    For Each cp In wb.VBProject.VBComponents
    For j = 0 To 3
    ProcedureExists = cp.CodeModule.ProcCountLines(procName, j) > 0
    If ProcedureExists Then Exit Function
    Next
    Next
    End Function[/VBA]

  5. #5
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Thanks Guys, you were a great help as always!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •