在 Excel 中使用 VBA 将多页中的元素从一页复制到另一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10822450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:20:46  来源:igfitidea点击:

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

excelvbamultipage

提问by Ehudz

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?

我在用户表单中有一个多页。在运行期间,用户可以随时选择添加 x 个页面。每个页面的元素都是相同的。我想知道是否有办法复制这些元素,或者我是否需要为每个新页面重新创建这些相同的元素?如果是这样,我如何在页面上指定应该放置元素的位置?

enter image description here

在此处输入图片说明

回答by Siddharth Rout

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

诀窍是将所有控件放在第 1 页的一个框架中,然后剩下的就变得简单了 :)

This code will copy the controls from Page1to Page2after creating Page2and align them accordingly.

此代码将进行复制的控制Page1,以Page2创建后Page2,并相应调整他们。

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

在此处输入图片说明

回答by Kazarenko

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.

“运行时错误 '-2147417949 (80010108)' 可能是由于窗体上的其他地方有一个框架。尝试删除任何其他框架并再次运行。