有一种方式来获得VB项目模块的Sub
是,如果你使用一个变量(或常量)以保存您的Sub
名称。
请参见下面的代码:
Option Explicit
Sub mySub()
Const PROC_NAME = "mySub"
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim i As Long
Dim ModuleName As String
Set VBProj = ActiveWorkbook.VBProject
' loop through all modules, worksheets and other objects in VB Project
For Each VBComp In VBProj.VBComponents
Set CodeMod = VBComp.CodeModule
' loop through all code line inside current module
For i = 1 To CodeMod.CountOfLines
If Len(CodeMod.Lines(i, 1)) > 0 Then
' if the name of current sub is found within the current code line
If InStr(CodeMod.Lines(i, 1), PROC_NAME) > 0 Then
ModuleName = CodeMod.Name '<-- get the current Module name
MsgBox "Sub " & PROC_NAME & " found in " & ModuleName & " module"
Exit Sub
End If
End If
Next i
Next VBComp
End Sub
为了访问VB项目模块,需要遵循以下两个步骤:
步骤1:添加“信托访问VBA项目对象模型“,转至开发者 >>宏安全 >>然后将V添加到信任访问VBA项目对象模型。
步骤2:添加引用您的VB项目中,添加 “的Microsoft Visual Basic应用程序扩展5.3”
就是这样,你就可以搏一搏 !
我知道这种方法,但我想要这样的东西,如果可用或可能的话; myvar = ActiveWorkbook.VBProject.CurrentSub.name –