如下图所示:
在vba工程中有窗体(UserForm1)、类模块(类1)、标准模块(模块1、模块2),文档模块(Sheet1、ThisWorkbook)等组件。
以下示例代码可以遍历所有组件,并输出所有组件的名称、类型以及声明代码的总行数和代码的总行数:
Const vbext_ct_ActiveXDesigner = 11 Const vbext_ct_ClassModule = 2 Const vbext_ct_Document = 100 Const vbext_ct_StdModule = 1 Const vbext_ct_MSForm = 3 Sub QQ1722187970() Dim oCM As CodeModule Dim oVC As VBComponent For Each oVC In Excel.ThisWorkbook.VBProject.VBComponents With oVC '输出组件的名称 Debug.Print .Name, Select Case .Type Case 11 Debug.Print "vbext_ct_ActiveXDesigner" Case 2 Debug.Print "vbext_ct_ClassModule" Case 100 Debug.Print "vbext_ct_Document" Case 1 Debug.Print "vbext_ct_StdModule" Case 3 Debug.Print "vbext_ct_MSForm" End Select Set oCM = .CodeModule With oCM '输出总的声明代码行数和总的代码行数 Debug.Print .CountOfDeclarationLines, .CountOfLines End With End With Next End Sub
发表评论