Solved

Determine all Excel WorkBooks with VBA code

Posted on 2011-03-23
4
374 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:thydzik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 35200548
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
 
LVL 11

Assisted Solution

by:thydzik
thydzik earned 0 total points
ID: 35205344
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
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 35211697
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
 
LVL 11

Author Closing Comment

by:thydzik
ID: 35238998
accepted my complete code as the solution. points awarded to zorvek for the expert help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question