2009-08-19 9 views
4

Ich schreibe ein Reporting-Tool Excel-Dateien für verschiedene „Compliance-Kriterien“, einschließlich wkb.VBProject.Protection zu dokumentieren, wenn der VBA gesperrt ist zu melden.Wie zum Testen der Existenz von VBA in Excel-Arbeitsmappe, in VBA?

Aber wie kann ich feststellen, wenn die Arbeitsmappe jedes Projekt?

Wenn ich

wkb.VBProject.VBComponents.Count - wkb.Worksheets.Count - 1 '(for the workbook) 

berechnen, die mir die Anzahl der Module + Klassenmodule + Formen geben, aber ich kann noch einige Codes hinter einem Blatt hat.

Gibt es einen Weg in Excel - wie Access frm.HasModule - um herauszufinden, ob es VBA-Code in der Arbeitsmappe gibt?

Antwort

5

Ich habe Folgendes verwendet, um die Gesamtanzahl der Zeilen in einem Projekt vorher zu zählen. Es wird Code in ThisWorkbook, Codemodule, Klassenmodule und Formulare abholen.

Private Sub countCodeLines() 
    Dim obj As Object 
    Dim VBALineCount As Long 
    For Each obj In ThisWorkbook.VBProject.VBComponents 
     VBALineCount = VBALineCount + obj.CodeModule.CountOfLines 
    Next obj 
    Debug.Print VBALineCount 
End Sub 

ist jedoch zu beachten, dass, wenn Ihre Arbeitsmappen Option Explicit gezwungen haben, dann wird dies als zwei Zeilen zählen pro Objekt (Option Explicit und einem Zeilenvorschub). Wenn Sie wissen, dass dies der Fall zu sein, und das LOC aus einem anderen Projekt einchecken, dann könnte man einfach die Anzahl der Objekte zählt, verdoppeln und testen, dass VBALineCount diese Nummer nicht überschreitet.

+0

dank Lunatik, das ist perfekt. –

4

Nach Lunatik des Hinweises, hier ist meine letzte Funktion (für wen es helfen):

 
Function fTest4Code(wkb As Workbook) As Boolean 
    'returns true if wkb contains VBA code, false otherwise 
    Dim obj As Object 
    Dim iCount As Integer 
    For Each obj In wkb.VBProject.VBComponents 
     With obj.CodeModule 
      '# lines - # declaration lines > 2 means we do have code 
      iCount = iCount + ((.CountOfLines - .CountOfDeclarationLines) > 2) 
     End With 
     If iCount 0 Then Exit For 'stop when 1st found 
    Next obj 
    fTest4Code = CBool(iCount) 
End Function 
7

Excel 2007+ hat eine neue Arbeitsmappe Eigenschaft namens „.HasVBProject“, die Sie fragen können.

Für Excel 2003 und früher ist der obige Lösungstest für Codezeilen im CodeModule eines der VBComponents der Arbeitsmappe geeignet.

Sie sollten die Eigenschaft ".CountOfLines" ganz alleine testen, da Codezeilen im Deklarationsabschnitt eines Codemoduls (erhalten über ".CountOfDeclarationLines") von Excel als "Makrocode" betrachtet werden und im Makro gespeichert werden müssen -fähige Formate.

Public Function HasVBProject(Optional pWorkbook As Workbook) As Boolean 
' 
' Checks if the workbook contains a VBProject. 
' 
On Error Resume Next 
    Dim wWorkbook As Workbook 
    Dim wVBComponent As VBIDE.VBComponent ' As Object if used with Late Binding 

    ' Default. 
    ' 
    HasVBProject = False 

    ' Use a specific workbook if specified, otherwise use current. 
    ' 
    If pWorkbook Is Nothing _ 
    Then Set wWorkbook = ActiveWorkbook _ 
    Else Set wWorkbook = pWorkbook 
    If wWorkbook Is Nothing Then GoTo EndFunction 

    If (VBA.CInt(Application.Version) >= 12) _ 
    Then 
     ' The next method only works for Excel 2007+ 
     ' 
     HasVBProject = wWorkbook.HasVBProject 
    Else 
     ' Signs the workbook has a VBProject is code in any of the VBComponents that make up this workbook. 
     ' 
     For Each wVBComponent In wWorkbook.VBProject.VBComponents 
      If (wVBComponent.CodeModule.CountOfLines > 0) _ 
      Then 
       ' Found a sign of programmer's activity. Mark and quit. 
       ' 
       HasVBProject = True: Exit For 
      End If 
     Next wVBComponent 
    End If 

EndFunction: 
    Set wVBComponent = Nothing 
    Set wWorkbook = Nothing 
End Function 

Dutch