Determine all Excel WorkBooks with VBA code

Experts,

I would like to find all Excel WorkBooks with any VBA code in them.
I have the following from
http://www.ozgrid.com/forum/showthread.php?t=58216&p=300667#post300667
But this only finds modules, how would I also determine if a sheet has VBA code?

Thanks
LVL 11
thydzikAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
zorvek (Kevin Jones)Connect With a Mentor ConsultantCommented:
Another reference that might help.

Title: Determining If A Worksheet Or Workbook Has Code
Link: http://spreadsheetpage.com/index.php/site/tip/determining_if_a_worksheet_or_workbook_has_code/

Kevin
0
 
thydzikConnect With a Mentor Author Commented:
zorvek, thanks for that;

final solution was

 
Sub getVBAWorkBooks()

Dim s As Worksheet
Set s = ThisWorkbook.Sheets("Listing")

s.Cells.Clear

Dim irow As Long

Dim folderPath As String
folderPath = "D:\"
If InStrRev(folderpatch, "\", -1, vbBinaryCompare) <> Len(folderPath) Then folderPath = folderPath & "\"

Dim fileStr As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False 'prevent workbook_open events

'find modules
Dim vbComp As VBComponent
Dim sht As Worksheet

Dim wb As Workbook
                
fileStr = Dir(folderPath & "*.xls")
Do While Len(fileStr) > 0
    Set wb = Workbooks.Open(folderPath & fileStr, xlUpdateLinksNever)
    For Each vbComp In wb.VBProject.VBComponents
        irow = irow + 1
        s.Cells(irow, "A").Value = fileStr
        s.Cells(irow, "B").Value = CompTypeToName(vbComp)
    Next vbComp
    
    
    For Each sht In wb.Worksheets
        If wb.VBProject.VBComponents(sht.CodeName).CodeModule.CountOfLines > 0 Then
            irow = irow + 1
            s.Cells(irow, "A").Value = fileStr
            s.Cells(irow, "B").Value = sht.Name
        End If
    Next sht

    Application.DisplayAlerts = False
    wb.Close
    Application.DisplayAlerts = True
    
    fileStr = Dir$()
Loop

Application.ScreenUpdating = True
End Sub

Function CompTypeToName(vbComp As VBComponent) As String
    Select Case vbComp.Type
        Case vbext_ct_ActiveXDesigner
            CompTypeToName = "ActiveX Designer"
        Case vbext_ct_ClassModule
            CompTypeToName = "Class Module"
        Case vbext_ct_Document
            CompTypeToName = "Document"
        Case vbext_ct_MSForm
            CompTypeToName = "MS Form"
        Case vbext_ct_StdModule
            CompTypeToName = "Standard Module"
        Case Else
    End Select
End Function

Open in new window

0
 
zorvek (Kevin Jones)ConsultantCommented:
I created my own version:

Public Function IsVBACodeInWorkbook( _
        ByVal Workbook As Workbook _
    ) As Boolean

' Return True if any VBA code exists in the workbook.

    Dim VBComponent As VBComponent
   
    For Each VBComponent In Workbook.VBProject.VBComponents
        Select Case VBComponent.Type
            Case vbext_ct_ActiveXDesigner
                IsVBACodeInWorkbook = True
            Case vbext_ct_ClassModule
                IsVBACodeInWorkbook = True
            Case vbext_ct_Document
                If VBComponent.CodeModule.CountOfLines > 0 Then
                    If VBComponent.CodeModule.CountOfLines = 2 Then
                        If VBComponent.CodeModule.Lines(1, 2) <> "Option Explicit" & vbCrLf Then
                            IsVBACodeInWorkbook = True
                        End If
                    Else
                        IsVBACodeInWorkbook = True
                    End If
                End If
            Case vbext_ct_MSForm
                IsVBACodeInWorkbook = True
            Case vbext_ct_StdModule
                IsVBACodeInWorkbook = True
            Case Else
        End Select
        If IsVBACodeInWorkbook Then Exit Function
    Next VBComponent
   
End Function

From a security perspective, Excel considers a workbook contains macros (VBA code) if any regular, form, or class module exists (even if it doesn't contain any code) or the ThisWorkbook or any sheet module contains any code beyond the default "Option Explicit" declaration line.

Kevin
0
 
thydzikAuthor Commented:
accepted my complete code as the solution. points awarded to zorvek for the expert help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.