Adding watches (2 Viewers)

ryetee

Registered User.
Local time
Today, 18:05
Joined
Jul 30, 2013
Messages
1,001
I'm testing/debugging a program
I'm setting up about 9 watches for the crucial part of testing
I have to end the program to test next part and have to reinput the 9 watches
Is there an easier way to do this other than reinput 1 by 1
 
Depending on exactly WHAT you want to view, I've often found MsgBox to be a great tool. In your code you can have a msgbox that shows what you need and, optionally, allow you to exit out of your code.
 
Found this from a quick Google

NB:The VBE must be in focus when the code is run. You can either do this manually or use the VBIDE object to set the focus.

Code:
' Source - https://stackoverflow.com/a
' Posted by Cor_Blimey, modified by community. See post 'Timeline' for change history
' Retrieved 2025-12-15, License - CC BY-SA 3.0

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
You need to make head and tail of it.
If it works, then you could use a table to get the data?
 
Last edited:
> Application.SendKeys "%DA"
This assumes you are in the VBA editor main window.
Sendkeys is always very fragile, but it may be your only resort since there is no programmatic access to the Watch window.
 
Sorry, I forgot to add that 'The VBE must be in focus when the code is run. You can either do this manually or use the VBIDE object to set the focus.'

Now added.
 

Users who are viewing this thread

Back
Top Bottom