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

  • Home
  • SEARCH
  • 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 5931699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:43:53+00:00 2026-05-22T14:43:53+00:00

I am trying to add a button to my worksheet during run-time. This button

  • 0

I am trying to add a button to my worksheet during run-time. This button should just display different worksheet that is also created on during run-time. I added button like this:

Dim btnShowTable
Set btnShowTable = ActiveSheet.Buttons.Add(rowRange.Left + 10, rowRange.Top + 10, rowRange.Width - 20, rowRange.Height - 20)
btnShowTable.Caption = "Show table data"
btnShowTable.OnAction = AddClickHandler_ShowSheet("ClickModule", "TableView", tableSheet)


Function AddClickHandler_ShowSheet(ByVal moduleName As String, ByVal btnName As String, ws As Worksheet)
  Dim methodName As String
  methodName = btnName & "_" & AddClickHandler_GetId() & "_Click"
  Dim LineNum As Long
  Dim VBCodeMod As CodeModule
  Set VBCodeMod = ThisWorkbook.VBProject.VBComponents(moduleName).CodeModule
  With VBCodeMod
    LineNum = .CountOfLines + 1
      .InsertLines LineNum, _
        "Sub " & methodName & "()" & Chr(13) & _
        "    " & ws.CodeName & ".Select" & Chr(13) & _
        "End Sub"
  End With
  AddClickHandler_ShowSheet = moduleName & "." & methodName
End Function

Function that creates the button is in one module while AddClickHandler_ShowSheet is in another.

The idea is that I would have separate module that would contain all these click handlers so that I could easily delete all of them.
This works ok. The handlers are created and buttons work as expected. The issue that I have is that, when this InsertLines method is called, all of my module variables in a module that contains the function for button creation are lost.
I have 4 module variables

Dim xmldoc As New MSXML2.DOMDocument
Dim xmlDataMap() As DataNode
Dim xmlDataMapLast As Integer
Dim xmlTables As Collection

after a call to InsertLines all of them became empty except for xmlDataMapLast which contains correct value of 14.

If I try to debug this method when I step over InserLines call I get an error “Can’t enter break mode at this time.” and I can’t debug anything until my function ends. If I comment out the call to AddClickHandler_ShowSheet my variables remain intact, so it must be something related to that call.

Am I trying to achieve the impossible or am I just doing it the wrong way?

  • 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-05-22T14:43:54+00:00Added an answer on May 22, 2026 at 2:43 pm

    It’s no surprise the module is reset.
    In fact, I would expect value types to reset, too. It’s a bit of surprise
    they survive.

    Why would you need to store variables in the module you use for code generation?
    Store them in a separate module and only use this module for code generation.


    Having that said, why would you dynamically add code in the first place?
    OnAction supports parameters:

    Sub asdff()
      Worksheets(1).Buttons(1).OnAction = "'Module1.ParametrizedHandler 5, ""Hi there""'"
    End Sub
    
    Public Sub ParametrizedHandler(ByVal foo As Long, ByVal bar As String)
      MsgBox foo, vbInformation, bar
    End Sub
    

    Note the absence of parentheses in the call string, and the single quotes around it.

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

Sidebar

Related Questions

I'm trying add a tab to my web page that looks like this: Using
I'm trying to add a button that contains the .closest & .remove into an
I'm trying to add a minimize button to my QDialog using this code in
I'm trying to add an Info button to the More screen that UITabBarController generates
I'm trying to add a button to the OpenLayers map that is supposed to
Im trying to add a button in my grid view that will redirect me
I'm trying to add a button to the uitableviewcell and i just can't get
I am trying to add Become-a-Fan button to my 2008 asp.net website (vb) Here
I am trying to set up an add-tab button in a Gtk::Notebook (gtkmm). I
I'm trying to add a label to my toolbar. Button works great, however when

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.