在Excel中使用VBA将元素从一页复制到另一页 [英] Copy Elements From One Page To Another in Multipage with VBA in Excel
问题描述
我在用户表单中有多页。在运行期间,用户可以随时选择添加x个页面。每个页面的元素将是相同的。我想知道是否有办法复制这些元素,还是需要为每个新页面重新创建相同的元素?如果是,请在页面上指定要放置元素的位置?
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?
推荐答案
诀窍是将所有控件放在一个框架中第一页,然后其余变得容易:)
The trick is to put all controls in a frame in the 1st page and then the rest becomes easy :)
此代码将将控件从 Page1
复制到 Page2
创建 Page2
并相应地对齐。
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
这篇关于在Excel中使用VBA将元素从一页复制到另一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!