Consulting

Results 1 to 3 of 3

Thread: VBa Code exist?

  1. #1
    VBAX Regular
    Joined
    Oct 2009
    Location
    Philippines
    Posts
    14
    Location

    Smile VBa Code exist?

    HEllo... help please!

    Can you please give some vba/macro codes on how to determine if an excel files contain a macro or vba code?

    Thank you very much...

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Perhaps this
    [VBA]Sub test()
    Dim myComp As Variant, isMacro As Boolean

    With Workbooks("Workbook2")
    For Each myComp In .VBProject.VBComponents
    isMacro = isMacro Or myComp.CodeModule.Find("End", 1, 1, 1, 1)
    Next myComp
    End With

    If isMacro Then
    MsgBox "There is code in this book"
    Else
    MsgBox "no code"
    End If
    End Sub[/VBA]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Expanding on Mike's code
    [VBA]
    Sub test()
    Dim myComp As Variant, isMacro As Boolean
    Dim wb As Variant
    msg1 = "There is code in: " & vbCr
    msg2 = "There is no code in: " & vbCr

    For Each wb In Workbooks
    isMacro = False
    With wb
    If UCase(Left(wb.Name, 8)) <> "PERSONAL" Then
    For Each myComp In wb.VBProject.VBComponents
    isMacro = isMacro Or myComp.CodeModule.Find("End", 1, 1, 1, 1)
    Next myComp

    If isMacro Then
    msg1 = msg1 & wb.Name & vbCr
    Else
    msg2 = msg2 & wb.Name & vbCr
    End If
    End If
    End With
    Next
    MsgBox msg1 & vbCr & msg2
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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