有一種方式來獲得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 –