Copy Elements From One Page To Another in Multipage with VBA in Excel
Asked Answered
S

2

6

I have a multipage in a userform. During run-time, the user can choose to add x number of pages at any time. The elements of each page will be the same. I am wondering if there is a way to duplicate these elements, or would I need to re-create these same elements for each new page? If so, how do I specify locations on the page where the element should be placed?

enter image description here

Servia answered 30/5, 2012 at 18:52 Comment(1)
You can duplicate them by copying and pasting or by creating from scratch.Inverness
I
9

The trick is to put all controls in a frame in the 1st page and then the rest becomes easy :)

This code will copy the controls from Page1 to Page2 after creating Page2 and align them accordingly.

Option Explicit

Private Sub CommandButton2_Click()
    Dim l As Double, r As Double
    Dim ctl As Control

    MultiPage1.Pages.Add

    MultiPage1.Pages(0).Controls.Copy
    MultiPage1.Pages(1).Paste

     For Each ctl In MultiPage1.Pages(0).Controls
        If TypeOf ctl Is MSForms.Frame Then
            l = ctl.Left
            r = ctl.Top
            Exit For
        End If
    Next

    For Each ctl In MultiPage1.Pages(1).Controls
        If TypeOf ctl Is MSForms.Frame Then
            ctl.Left = l
            ctl.Top = r
            Exit For
        End If
    Next
End Sub

SNAPSHOT

enter image description here

Inverness answered 30/5, 2012 at 19:21 Comment(6)
When I run the above code, I get a "Run-time error '-2147417949 (80010108)': Automation error the object invoked as diconnected from its clients."Servia
which line is giving you the error? I just tested it again and it works.Inverness
if I comment everything out and leave just MultiPage1.Pages.Add it gives me the errorServia
That's strange. Can you upload thefile that you created just now and upload it in say wikisend.com and then share the link here.Inverness
I think there's some underlying issue affecting all my userforms in that same project. I created a new project add userform and tried the above code and it works. I'll try re-creating the userform and see what happensServia
You don´t actually have to use a frame if you are just going to replicate the page inside the Multipage. Just use controls.copy and then .paste and that should do.Awoke
G
1

The "Run-time error '-2147417949 (80010108)' may be caused by having a Frame somewhere else on the form. Try removing any other frames and running again.

Giffin answered 8/9, 2014 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.