I have this short piece of code
Public n As Integer
Public Sub Foo()
For i = 0 To 4
MyModule.n = MyModule.n + 1
Next i
End Sub
which is defined in a Module named MyModule
. This code is working as expected: After executing for the first time 'MyModule.n' has the value 5. After executing the second time it has the value 10 and so on.
When I extend the code, to add a CommandButton and place it onto the working sheet, the global variable MyModule.n
loses it's value on every new call of Foo
:
Public n As Integer
Public Sub Foo()
Dim btn As OLEObject
For i = 0 To 4
Set btn = Worksheets("Aufträge").OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Left:=122, Top:=321, Width:=30, Height:=30)
MyModule.n = MyModule.n + 1
Next i
End Sub
The code seems to work because the command button is created and placed correctly. Why does the global variable reset if executing the second code fragment?
Furthermore I can't place a break point after or inside the For-Loop in the second code fragment. I get the message Can't enter break mode at this time
.