如何手动保存VBA手表或通过代码添加它们?

Chr*_* B. 10 excel vba excel-vba

我有相当数量的全局变量,并在调试期间跟踪它们我使用手表.然而,在每个会话开始时一次又一次地添加所有这些都很烦人.有没有办法保存和加载它们?或者,如果无法通过代码添加它们?

这个问题涉及VBA编辑器窗口中表达式的监视(参见屏幕截图). 截图

Nat*_*Sav 8

在一个名为WatchedVariables的类中,您可以拥有

Option Explicit

Private str_Variable1_Prev As String
Private str_Variable1 As String

Public Property Let strVariable1(value As String)
    str_Variable1_Prev = str_Variable1
    str_Variable1 = value
    If str_Variable1 <> str_Variable1_Prev Then
        Debug.Print "Variable 1 has changed"
    Else
    End If
End Property
Run Code Online (Sandbox Code Playgroud)

然后你就可以访问你观察到的变量了,比如clsWatchedVariables.strVariable1 ="nathan"

但是,这可能有助于在VBA监视列表中查找特定文本


Cor*_*mey 6

它不是很漂亮,但你可以使用Application.SendKeys来添加手表.我不知道语言区域设置是否会影响快捷键.以下适用于英语.

限制是你只能使用当前模块或所有模块之类的东西(即一直向上或向下滚动),但不能用于范围的特定其他模块.您可以通过使用VBIDE查找有多少模块等来修复此限制,因此可以向上滚动特定模块的次数.我没有为下面的代码做这个,因为这是一个概念证明 - 我把有趣的部分留给你:-)

用法:使用指定的参数调用AddWatch子.您可以在启动新会话时将这些添加到您调用的子项中,如我的"HelloNewSession()"中所示.

运行代码时,VBE必须处于焦点.您可以手动执行此操作,也可以使用VBIDE对象设置焦点.

Option Explicit

Enum enumWatchType
    WatchExpression
    BreakWhenTrue
    BreakWhenChange
End Enum
Enum enumProceduresType
    AllProcedures
    Caller
End Enum
Enum enumModuleType
    AllModules
    CurrentModule
    ThisWorkbook
End Enum

Public testVar As Boolean
Sub HelloNewSession()
    AddWatch "testVar = True", AllProcedures, CurrentModule, BreakWhenTrue
    testVar = True
End Sub

Sub AddWatch( _
    expression As String, _
    Optional proceduresType As enumProceduresType = enumProceduresType.Caller, _
    Optional moduleType As enumModuleType = enumModuleType.CurrentModule, _
    Optional watchType As enumWatchType = enumWatchType.WatchExpression)

    Dim i As Long

    Application.SendKeys "%DA"
    Application.SendKeys getEscapedSendkeysText(expression)
    If proceduresType = enumProceduresType.AllProcedures Then
        Application.SendKeys "%p"
        For i = 1 To 1000 'You could use VBIDE to count the valid types to actually scroll up the right number of times!
            Application.SendKeys "{UP}"
        Next
    End If
    If moduleType = enumModuleType.AllModules Then
        Application.SendKeys "%m"
        For i = 1 To 1000 'You could use VBIDE to count the valid types to actually scroll up the right number of times!
            Application.SendKeys "{UP}"
        Next
    ElseIf moduleType = enumModuleType.ThisWorkbook Then
        Application.SendKeys "%m"
        For i = 1 To 1000 'You could use VBIDE to count the valid types to actually scroll up the right number of times!
            Application.SendKeys "{DOWN}"
        Next
    End If
    Select Case watchType
        Case enumWatchType.WatchExpression
            Application.SendKeys "%w"
        Case enumWatchType.BreakWhenTrue
            Application.SendKeys "%t"
        Case enumWatchType.BreakWhenChange
            Application.SendKeys "%c"
    End Select

    Application.SendKeys "~"

End Sub
Function getEscapedSendkeysText(ByVal text As String) As String
    Dim char As String, i As Long
    Const chars As String = "~%+^()[]"
    For i = 1 To Len(chars)
        char = Mid$(chars, i, 1)
        text = Replace(text, char, "{" & char & "}")
    Next
    getEscapedSendkeysText = text
End Function
Run Code Online (Sandbox Code Playgroud)