允许用户在Excel多页面用户窗体中复制页面

我在这里发布这个问题,因为我实际上发现了另一个类似于这个问题,“复制元素从一个页面到另一个Excel中的VBA与多页”我仍然有问题,因为我不知道这将如何工作。 不过,我仍然需要将我的数据字段放入一个框架中。

我需要允许用户根据需要添加尽可能多的页面,而一些用户的内容将随之移动,而有些则不会。

有一个用户告诉系统有多less次需要复制它的字段会更好吗? 或者我应该只是有一个button,说:“复制此页面”

此外,除了UserForm页面被重复用于用户input,我还需要该过程来复制Excel工作表以关联Userform中的每个重复的页面。

帮助与此将不胜感激!

设置上限。 即一个用户不应该能够添加超过特定数量的页面。 这将确保您不会遇到未知的错误。 例如,用户不应该添加超过25页(只是一个例子)。请记住,当添加无限制的工作表时,Excel在内存方面非常饿,因此定义一个上限是必要的。 另外想象一下,在多页面中说100页。 这太乱了。

有一个用户告诉系统有多less次需要复制它的字段会更好吗? 或者我应该只是有一个button,说:“复制此页面”

我相信你可以有两个select。 这将确保

  1. 如果用户想要一次添加6页,则用户不必按6次button。
  2. 如果用户只想添加一个页面,则用户可以简单地点击该button,而不是在文本框中input“1”,然后点击复制button。

另外,除了UserForm页面被复制以供用户input之外,我还需要该过程来复制Excel工作表以关联Userform中的每个重复页面。

  1. 这再次不是一个问题。 您始终可以使用Worksheets.Add方法来添加更多的工作表
  2. 然后,您可以将页面上每个控件的来源设置为相应的页面。

这是一个简单的例子

这将创build一个页面的副本,并将现有的文本框复制到新的页面,并设置它的ControlSource属性。

屏幕截图

之前

在这里输入图像说明

在这里输入图像描述

 Option Explicit Private Sub CommandButton1_Click() Dim pgCount As Long Dim wsNew As Worksheet Dim ctl As Control '~~> Get the current count of the pages pgCount = MultiPage1.Pages.Count '~~> Add a new page MultiPage1.Pages.Add '~~> Change '0' to whatever page you want to copy from MultiPage1.Pages(0).Controls.Copy '~~> Paste it in the newly created multipage MultiPage1.Pages(pgCount).Paste '~~> Add a new sheet Set wsNew = ThisWorkbook.Sheets.Add '~~> Give the new sheet a name wsNew.Name = "Erika" '~~> Adding a test value in Range A1 so that it reflects '~~> When we set the ControlSource of the textbox wsNew.Range("A1").Value = "Hello World" For Each ctl In MultiPage1.Pages(pgCount).Controls If TypeOf ctl Is MSForms.TextBox Then ctl.ControlSource = wsNew.Range("A1").Address Exit For End If Next End Sub 

这里是另一个例子,而不是复制控件,你可以创build一个新的控件,并设置它的ControlSource属性。

屏幕截图

(同上)

 Option Explicit Private Sub CommandButton1_Click() Dim pgCount As Long Dim wsNew As Worksheet Dim ctl As Control '~~> Get the current count of the pages pgCount = MultiPage1.Pages.Count '~~> Add a new page MultiPage1.Pages.Add '~~> Add a new textbox Set ctl = MultiPage1.Pages(pgCount).Controls.Add("Forms.TextBox.1", _ "TextBox" & pgCount) '~~> Add a new sheet Set wsNew = ThisWorkbook.Sheets.Add '~~> Give the new sheet a name wsNew.Name = "Erika" '~~> Adding a test value in Range A1 so that it reflects '~~> When we set the ControlSource of the textbox wsNew.Range("A1").Value = "Hello World" ctl.ControlSource = wsNew.Range("A1").Address End Sub 

希望这会让你开始…