mercredi 1 avril 2015

How to keep public static variable from becoming unassigned?

I want to have a Dictionary dict available to a worksheet event handler, so I've stored it in a module GlobalVariables as follows:



Public dict As Dictionary


dict is initialized in the Workbook_Open event:



Private Sub workbook_open()

Set dict = New Dictionary
dict.Add "abc", "def"

End Sub


This is the event handler in Sheet1:



Private Sub worksheet_beforedoubleclick(ByVal target As Range, cancel As Boolean)

If dict Is Nothing Then
Debug.Print "nothing"
Else
Debug.Print "not nothing"
End If

End Sub


When I first open the workbook and double-click on a cell in Sheet1, dict is not Nothing. But If I put a breakpoint on this line:



If dict Is Nothing Then


and then End the sub after it halts at the breakpoint, then the next time I double-click and reach the same breakpoint, dict is Nothing. It seems that ending the _beforedoubleclick sub prematurely has the effect of unassigning dict. How can I keep this from happening? I'd like to have this public variable keep its value while I'm debugging.


Aucun commentaire:

Enregistrer un commentaire