How do you control a button inside a multipage in excel VBA

12,889

You probably would be better off creating a button in the ribbon so that it is available on all pages:

http://chandoo.org/wp/2012/02/27/how-to-add-your-own-macros-to-excel-ribbon/

EDIT:

My bad, I thought you meant a worksheet instead of a VBA MultiPage in a userform.

Check this out. I was able to make this work for me:

Assign code to a button created dynamically

Class1:

Option Explicit

Public WithEvents CmdEvents As MSForms.CommandButton

Private Sub CmdEvents_Click()
    MsgBox "yo"
End Sub

Userform with MultiPage object:

Option Explicit

Dim cmdArray() As New Class1

Private Sub CommandButton1_Click()
    Dim newControl As Control

    Set newControl = Me.MultiPage1.Pages(0).Controls.Add("Forms.CommandButton.1", "NewCommand", True)

    newControl.Object.Caption = "hello"


    newControl.Left = 50
    newControl.Top = 50

    ReDim Preserve cmdArray(1 To 1)
    Set cmdArray(1).CmdEvents = newControl

    Set newControl = Nothing
End Sub
Share:
12,889

Related videos on Youtube

Jovanni G
Author by

Jovanni G

I have more than 6 years of extensive knowledge in building websites and web applications. I started out as a web designer and moved my way up to web development since I am good with problem solving and have good analytical skills. Eventually, from web designing, I ventured into Wordpress, then got into CodeIgniter, then finally settled with Laravel+VueJS development and got really comfortable with it. I am very procient with programming languages such as PHP, CSS, HTML, and Javascript, and I always prefer future proof solutions over speed. I am also well experienced with Google API Libraries, Stripe API, Spark Platform API and other API services available as well as building RESTful APIs.

Updated on June 04, 2022

Comments

  • Jovanni G
    Jovanni G almost 2 years

    I have a multipage, I was successfully able to copy elements of the first page which is my reference page to new pages which is created dynamically.

    My question is, how do I set a commandbutton's actions inside a page in a multipage control? My goal is to click on the button from any page then pops up another form.

    How do I do this? It's pretty hard to adjust from Android to VB. I really appreciate any help from you guys.

    This is my code in cloning pages.

    i = 0
    MultiPage1.Pages.Add
    MultiPage1.Pages(i).Controls.Copy
    i = i + 1
    MultiPage1.Pages(i).Paste
    For Each ctl In Me.MultiPage1.Pages(i).Controls
         If TypeOf ctl Is MSForms.Label Then
               '~~~ code omitted
               Select Case ctl.Tag
                    Case "startTime"
                            ctl.Caption = "4:00pm"
               End Select
         End If
    Next
    

    this is how it's going to look like. enter image description here

    the button will concatenate all strings inside the page. the concatenated string will be shown on another userform.

  • Jovanni G
    Jovanni G about 11 years
    Thanks for you response Peter, I don't somehow understand what to put on the "`your code here". The buttons were a clone from the reference page which is Page(0) on the Multipage. And the pages as well as it's contents are dynamically created. I can post my code here if you'd like.
  • Peter Albert
    Peter Albert about 11 years
    @JovanniG: How do you clone the page? Maybe you can access the button with something like this: Set objYourButton = MyMultipage.Pages(MyMultipage.Pages.Count).Controls(1) You might need to replace the 1 with the number for the button...
  • Jovanni G
    Jovanni G about 11 years
    I see. Now I get it. I will try this one out. Thanks a lot.
  • Peter Albert
    Peter Albert about 11 years
    @JovanniG: Great! Let me know how it went!
  • Jovanni G
    Jovanni G about 11 years
    I tried your suggestion, but it did not worked. I added images on my question and a code snippet to clarify everything as well.