Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8478103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:39:08+00:00 2026-06-10T18:39:08+00:00

I have this short piece of code Public n As Integer Public Sub Foo()

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:39:09+00:00Added an answer on June 10, 2026 at 6:39 pm

    Based on the search I did & my conclusion is that you can’t add controls dynamically to the worksheet and retain the state of variables.

    Here is why: Adding a button will force the sheet to goto design mode & hence reset of variables.

    Supporting links

    1. http://www.pcreview.co.uk/forums/dynamically-adding-activex-controls-via-vba-kills-global-vba-heap-t3763287p2.html

    2. https://web.archive.org/web/20101215134333/http://support.microsoft.com/kb/231089 (originally //support.microsoft.com/kb/231089)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a short piece of code like this: typedef struct { double sX;
I have this simple piece of code public ActionResult ListToGrid(string field, string direction) {
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
I have a short question i have wrote this in java. Old code: class
I have a short little piece of code to play tones via Tao.Sdl. It
I have this piece of code that fire the alarm once by setting a
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have modified a short piece of pyqt code to produce real-time rendering of
I have a short question. I want do put this \tssr>&\8=f23' into a String
So I have been working on this project for a short while now. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.