2013-02-20 27 views
2

我創建了一個包含動態頁面的多頁面。當用戶窗體啓動時,如果用戶窗體是空的,它將檢查列中特定單元格上的值。然後爲每個非空單元創建一個頁面。如何動態地將單元格值放入excel的多頁文本框中?

這裏是我的代碼片斷

Private Sub UserForm_Initialize() 
    Dim i As Integer 
    Dim custDate As Date 
    Dim vID As String 
    'ActiveWorkbook.Worksheets("Sheet1").Activate 

    i = 0 

    custDate = DateValue(Now) 

    Range("A1").Offset(1, 0).Select 
    Do While Not IsEmpty(ActiveCell.Value) 
     'MsgBox ActiveCell.Address 
     If custDate = ActiveCell.Value Then 'first column(A) are dates 
      MultiPage1.Pages.Add 
      MultiPage1.Pages(0).Controls.Copy 'page 1 is the reference page 
      i = i + 1 'row counter 
      ActiveCell.Offset(0, 2).Select 'go to column(C) on the same row where visit ids are located 
      vID = ActiveCell.Value 
      MultiPage1.Pages(i).Paste 'copy page 1 contents to new page for each row on the active worksheet 

      'I guess this is where you put the code to put values 
      'on a txtbox that was from the reference page which is page 1 

      ActiveCell.Offset(0, -2).Select 'go back to column(A) to check back dates 

     End If 
     ActiveCell.Offset(1, 0).Select 'move to the next row 
    Loop 

    MultiPage1.Value = i 'select the new page on the userform 

End Sub 

現在我的問題是如何把值從細胞到從參考隱藏頁面複製到動態創建新頁面的文本框。我剛剛開始編程VBA昨晚。我是一名Android應用程序開發人員,所以從這一刻開始很難調整。

+0

'MultiPage1.Pages(0).Controls.Copy'是這個文本框要複製? – 2013-02-20 08:59:14

+0

不,這是將被複制的頁面,那麼與參考頁面具有相同內容的新頁面將具有更新單元格值的更新。 – 2013-02-20 09:52:05

+0

該頁面中有多少個文本框? – 2013-02-20 09:53:20

回答

2

我想這是你正在嘗試?

後粘貼控制,試試這個

' 
'~~> Rest of your code 
' 
MultiPage1.Pages(i).Paste 

For Each ctl In Me.MultiPage1.Pages(i).Controls 
    If TypeOf ctl Is MSForms.TextBox Then 
     '~~> Your code here 
     ctl.Text = vID 
     Exit For 
    End If 
Next 
' 
'~~> Rest of your code 
' 

如果您有多個同時聲明這是你的代碼的頂部

Dim ctl As Control 

隨訪(從評論)

Controls相同類型,我寧願不復制和粘貼,但從頭開始重新創建它們。這使我對這些控件有更多的瞭解Controls

但是,如果您仍想使用複製粘貼方法,請使用.Tag屬性。看到這個例子

創建一個userform,如下面的快照所示。

在Page(0)中爲每個文本框設置標籤。

enter image description here

讓我們使用這個代碼在用戶窗體

Option Explicit 

Dim ctl As Control 

Private Sub CommandButton1_Click() 

    Debug.Print "Page (0):-" 
    For Each ctl In Me.MultiPage1.Pages(0).Controls 
     If TypeOf ctl Is MSForms.TextBox Then 
      Debug.Print ctl.Name; "==="; ctl.Tag 
     End If 
    Next 

    Debug.Print "---" 
    Debug.Print "Page (1):-" 

    MultiPage1.Pages(0).Controls.Copy 

    MultiPage1.Pages.Add 
    MultiPage1.Pages(1).Paste 

    For Each ctl In Me.MultiPage1.Pages(1).Controls 
     If TypeOf ctl Is MSForms.TextBox Then 
      Debug.Print ctl.Name; "==="; ctl.Tag 
     End If 
    Next 

    Debug.Print "---" 
    Debug.Print "Page (2):-" 

    MultiPage1.Pages.Add 
    MultiPage1.Pages(2).Paste 
    For Each ctl In Me.MultiPage1.Pages(2).Controls 
     If TypeOf ctl Is MSForms.TextBox Then 
      Debug.Print ctl.Name; "==="; ctl.Tag 
     End If 
    Next 
End Sub 

當您運行的代碼,你會看到在屏幕

enter image description here

此輸出如果您發現.Tag不會更改。所以如果我們有更多的控制,我們可以有效地使用它。看到這個例子

Option Explicit 

Dim ctl As Control 

Private Sub CommandButton1_Click() 
    MultiPage1.Pages(0).Controls.Copy 

    MultiPage1.Pages.Add 
    MultiPage1.Pages(1).Paste 

    For Each ctl In Me.MultiPage1.Pages(1).Controls 
     If TypeOf ctl Is MSForms.TextBox Then 
      Select Case ctl.Tag 
       Case "A" 
        '~~> Your code goes here to put text in this textbox 
        ctl.Text = "AAAA" 
       Case "B" 
        '~~> Your code goes here to put text in this textbox 
        ctl.Text = "BBBB" 
      End Select 
     End If 
    Next 
End Sub 

當你運行它,你就會得到

enter image description here

HTH

+0

感謝您的快速回復,我會繼續檢查他的代碼。 – 2013-02-20 09:52:30

+0

OMG ...它的工作就像我希望的那樣。非常感謝。但是如果我在頁面上有兩個文本框呢?我怎麼知道哪一個會被複制到適當的文本框中? – 2013-02-20 09:58:05

+0

非常感謝你Siddharth。現在我終於走上了正軌。你的指示非常清晰。你搖滾。 – 2013-02-20 10:34:21

相關問題