2012-11-26 58 views
3

我在這裏發佈此問題是因爲我實際上發現了另一個類似於此問題的問題,「使用Excel中的VBA將多個元素從一個頁面複製到另一個頁面中的多頁中」仍然有問題,因爲我不知道這將如何工作。不過,我仍然需要將數據字段放入框架中。允許用戶在Excel中複製頁面多頁用戶表格

我需要允許用戶根據需要添加儘可能多的頁面,而一些用戶的內容會隨之移動,而有些則不會。

有一個字段,用戶告訴系統他們需要多少次複製它?或者,我應該只是有一個按鈕,說:「複製此頁」

此外,除了用戶輸入重複UserForm頁面,我需要的過程還複製Excel工作表,以關聯每個重複的頁面用戶表單。

幫助與此將不勝感激!

回答

0

設置上限。即用戶不應該能夠添加超過特定數量的頁面。這將確保您不會遇到未知的錯誤。例如,用戶不應該能夠添加超過25頁(僅舉一個例子)。請記住,當涉及添加無限制工作表時,Excel在內存方面非常餓,因此需要定義上限。另外想象一下,在多頁面中說100頁。這太亂了。

有一個字段,用戶告訴系統有多少次需要複製它?或者我應該只是有一個按鈕,說:「複製此頁」

我相信你可以有兩個選項。這將確保

  1. 如果用戶想要一次添加說6頁,那麼用戶不必按下按鈕6次。
  2. 如果用戶只想添加一個頁面,那麼用戶只需點擊該按鈕,而不是在文本框中輸入'1',然後點擊重複按鈕。

此外,除了用戶窗體頁被複制用戶輸入,我需要的過程中也複製Excel工作表中與用戶窗體中的每個頁面複製到assocate。

  1. 這又不是問題。您始終可以使用Worksheets.Add方法添加更多工作表
  2. 然後,您可以將頁面上每個控件的來源設置爲相應工作表。

下面是一個簡單的例子

這將創建頁的副本以及現有的文本複製到新的頁面,並設置其屬性ControlSource

SCREENSHOT

BEFORE

enter image description here

AFTER

enter image description here

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 

希望這可以讓你開始...

相關問題