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

会有一股神秘感。 提交于 2019-11-26 23:18:55

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/10822450/copy-elements-from-one-page-to-another-in-multipage-with-vba-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!