設置上限。即用戶不應該能夠添加超過特定數量的頁面。這將確保您不會遇到未知的錯誤。例如,用戶不應該能夠添加超過25頁(僅舉一個例子)。請記住,當涉及添加無限制工作表時,Excel在內存方面非常餓,因此需要定義上限。另外想象一下,在多頁面中說100頁。這太亂了。
有一個字段,用戶告訴系統有多少次需要複製它?或者我應該只是有一個按鈕,說:「複製此頁」
我相信你可以有兩個選項。這將確保
- 如果用戶想要一次添加說6頁,那麼用戶不必按下按鈕6次。
- 如果用戶只想添加一個頁面,那麼用戶只需點擊該按鈕,而不是在文本框中輸入'1',然後點擊重複按鈕。
此外,除了用戶窗體頁被複制用戶輸入,我需要的過程中也複製Excel工作表中與用戶窗體中的每個頁面複製到assocate。
- 這又不是問題。您始終可以使用
Worksheets.Add
方法添加更多工作表
- 然後,您可以將頁面上每個控件的來源設置爲相應工作表。
下面是一個簡單的例子
這將創建頁的副本以及現有的文本複製到新的頁面,並設置其屬性ControlSource
。
SCREENSHOT
BEFORE
AFTER
CODE
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
下面是另一個例子,而不是複製的控制,實際上就可以創建一個新的控制和設置它的ControlSource
財產。
SCREENSHOT
(同上)
CODE
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
希望這可以讓你開始...