使用类和#if 将使“向每个方法添加代码”更容易一些......
探查器类模块::
#If PROFILE = 1 Then
Private m_locationName As String
Private Sub Class_Initialize()
m_locationName = "unknown"
End Sub
Public Sub Start(locationName As String)
m_locationName = locationName
MsgBox m_locationName
End Sub
Private Sub Class_Terminate()
MsgBox m_locationName & " end"
End Sub
#Else
Public Sub Start(locationName As String)
'no op
End Sub
#End If
其他一些代码模块:
' helper "factory" since VBA classes don't have ctor params (or do they?)
Private Function start_profile(location As String) As Profiler
Set start_profile = New Profiler
start_profile.Start location
End Function
Private Sub test()
Set p = start_profile("test")
MsgBox "do work"
subroutine
End Sub
Private Sub subroutine()
Set p = start_profile("subroutine")
End Sub
在项目属性中将条件编译参数设置为:
PROFILE = 1
删除正常、非配置版本的行。
添加行很痛苦,我不知道有什么方法可以自动获取当前方法名称,这将使向每个函数添加分析行变得容易。您可以使用 VBE 对象模型为您注入代码 - 但我想知道手动执行此操作最终会更快。