excel - 如何动态地将单元格值放入Excel中多页的文本框中?

标签 excel vba dynamic copy multipage

我创建了一个带有动态页面的多页面。启动用户窗体时,用户窗体会检查列中特定单元格上的值是否为空。然后为每个非空单元格创建一个页面。

这是我的代码片段

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 应用程序开发人员,所以目前为止有点难以适应。

最佳答案

我认为这就是你正在尝试的?

粘贴控件后,试试这个

'
'~~> 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

跟进(来自评论)

如果您有多个相同类型的控件,那么我不喜欢复制和粘贴,而是从头开始重新创建它们。这使我可以更好地控制这些控件

但是,如果您仍想使用复制 - 粘贴方法,请使用 .Tag 属性。请参阅此示例

创建一个用户表单,如下面的快照所示。

在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

关于excel - 如何动态地将单元格值放入Excel中多页的文本框中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14975702/

相关文章:

使用 Open XML SDK 保护 Excel 文件密码

html - 代码中的 trs 导致运行时错误

excel - 将文件另存为 CSV UTF8 会出现错误 1004

tsql - 如何有条件地过滤 WHERE 子句中的列?

Spring Security 中的动态角色层次结构

Excel VBA - 检查工作簿中是否存在特定工作表和命名单元格的函数

VBA 将范围另存为工作簿

使用多个标准的 VBA 自动筛选

vba - 如何使用 R for 循环定义范围来创建图形

Java EE 7 动态用户角色