+ Reply to Thread
Results 1 to 6 of 6

Assigning an OnClick procedure to a dynamic CommandButton with VBA

  1. #1
    Registered User
    Join Date
    10-02-2022
    Location
    Ejpovice 200
    MS-Off Ver
    Office 365
    Posts
    2

    Assigning an OnClick procedure to a dynamic CommandButton with VBA

    I would like to create a macro within a UserForm that would create a CommandButton with a certain caption and assigns an OnClick event to it. I cannot figure out how to assign the macro . It should look like something like this. I would appreciate any ideas how to go about this.

    Private Result as String
    -----------------------------
    Sub VytvorForumular()
    Dim NewButton As OLEObject
    Set CmdBtn = ButtonForm.Controls.Add("Forms.CommandButton.1")
    With CmdBtn
    .Caption = "Value1"
    .onclick = SaveSelInVariable(.Caption) 'This is the part I cannot figure out - I just made this syntax up
    End With
    end sub
    ---------------------------
    Sub SaveSelInVariable (SelValue as String)
    Result = SelValue
    end sub

  2. #2
    Forum Contributor
    Join Date
    05-09-2021
    Location
    Sweden
    MS-Off Ver
    Office 365
    Posts
    148

    Re: Assigning an OnClick procedure to a dynamic CommandButton with VBA

    Hi,

    Using a quick google search I found that the it should be .onAction instead of .onclick

    https://stackoverflow.com/questions/...n-vba/31684761

    Does this help?

  3. #3
    Registered User
    Join Date
    10-02-2022
    Location
    Ejpovice 200
    MS-Off Ver
    Office 365
    Posts
    2

    Re: Assigning an OnClick procedure to a dynamic CommandButton with VBA

    Thanks for the reply. Using .OnAction does not work. I keep receiving compile errors. :-(

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,218

    Re: Assigning an OnClick procedure to a dynamic CommandButton with VBA

    Do you want to create the button at runtime (i.e., when the UserForm is already loaded), or in design mode (i.e., before you run your code)?

    Artik

  5. #5
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Assigning an OnClick procedure to a dynamic CommandButton with VBA


  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: Assigning an OnClick procedure to a dynamic CommandButton with VBA

    Buttons on a form do not have an onaction or onclick. If you need to create them at runtime, you can use a class with a WithEvents variable to handle (most of) its events. There are many examples in this forum.
    Rory

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Call procedure when I click any commandbutton
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-22-2012, 04:33 AM
  2. Dynamic onclick for checkbox
    By capetownandre in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-30-2009, 06:10 AM
  3. Using A Macro To Add Event Procedure To A CommandButton
    By Donna in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-23-2005, 11:06 AM
  4. [SOLVED] Using A Macro To Add Event Procedure To A CommandButton
    By Donna in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-21-2005, 11:06 AM
  5. dynamic hyperlink or onclick event
    By Tasha in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-16-2005, 10:42 AM
  6. [SOLVED] what is right or wrong-Using A Macro To Add Event Procedure To A CommandButton
    By Donna in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-09-2005, 07:58 AM
  7. Programming To The VBE -Using A Macro To Add Event Procedure To A CommandButton
    By DonnaG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-08-2005, 05:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1