如何在基于Table的用户窗体中自动生成文本?
How to make auto-generated Text in UserForm Based on Table?
我创建了一个 Table1,它使用 UserForm 通过按下(添加新记录)按钮添加新行,一旦我 select 来自 Combobox (comboName) 的名称,(txtDiscipline ) 将根据指定 For each name a discipline 的 Table2 自动生成(自动出现)。
Table1 and Table2
The UserForm for adding Rows in Table1
更新:
The Message after closing the userform
您可以在用户窗体中创建 2 个子项,并在组合框更改时使用查找方法。请注意,如果您在 F 列中有相同的名称,这将不起作用,然后您需要为每个项目定义唯一的 ID。
Private Sub comboName_Change()
Dim int_LR As Integer: int_LR = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row ' find last row of table
Dim str_Name As String: str_Name = Me.comboName ' look up this name
Dim rng_FindRange As Range ' search range
Set rng_FindRange = ActiveSheet.Range("F3:G" & int_LR).find(str_Name)
If Not rng_FindRange Is Nothing Then
Me.txtDiscipline = ActiveSheet.Cells(rng_FindRange.Row, 7)
Else ' If Not rng_FindRange Is Nothing
Me.txtDiscipline = "Name not found"
End If ' If Not rng_FindRange Is Nothing
End Sub
当用户窗体初始化时
Private Sub UserForm_Initialize()
Dim int_LR As Integer: int_LR = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row ' find last row of table
Me.comboName.Style = fmStyleDropDownList ' combobox list style
For i = 3 To int_LR ' loop adding items to combobox
Me.comboName.AddItem ActiveSheet.Cells(i, 6)
Next i
End Sub
我创建了一个 Table1,它使用 UserForm 通过按下(添加新记录)按钮添加新行,一旦我 select 来自 Combobox (comboName) 的名称,(txtDiscipline ) 将根据指定 For each name a discipline 的 Table2 自动生成(自动出现)。
Table1 and Table2
The UserForm for adding Rows in Table1
更新:
The Message after closing the userform
您可以在用户窗体中创建 2 个子项,并在组合框更改时使用查找方法。请注意,如果您在 F 列中有相同的名称,这将不起作用,然后您需要为每个项目定义唯一的 ID。
Private Sub comboName_Change()
Dim int_LR As Integer: int_LR = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row ' find last row of table
Dim str_Name As String: str_Name = Me.comboName ' look up this name
Dim rng_FindRange As Range ' search range
Set rng_FindRange = ActiveSheet.Range("F3:G" & int_LR).find(str_Name)
If Not rng_FindRange Is Nothing Then
Me.txtDiscipline = ActiveSheet.Cells(rng_FindRange.Row, 7)
Else ' If Not rng_FindRange Is Nothing
Me.txtDiscipline = "Name not found"
End If ' If Not rng_FindRange Is Nothing
End Sub
当用户窗体初始化时
Private Sub UserForm_Initialize()
Dim int_LR As Integer: int_LR = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row ' find last row of table
Me.comboName.Style = fmStyleDropDownList ' combobox list style
For i = 3 To int_LR ' loop adding items to combobox
Me.comboName.AddItem ActiveSheet.Cells(i, 6)
Next i
End Sub