Vba 使用当前行中的记录填充userform
我有一个带有多个文本框、单选按钮、下拉列表等的userform。当用户创建一个新条目时,数据保存在数据表上,一条记录占据一行。现在我希望能够单击A列中的“编辑”按钮,该按钮允许加载预装有此行数据的userform 问题是,当加载表单时,初始化宏会将所有表单字段重置为“”,我还不知道如何告诉VBA加载调用行的数据 有什么关于如何进行的建议吗Vba 使用当前行中的记录填充userform,vba,excel,userform,Vba,Excel,Userform,我有一个带有多个文本框、单选按钮、下拉列表等的userform。当用户创建一个新条目时,数据保存在数据表上,一条记录占据一行。现在我希望能够单击A列中的“编辑”按钮,该按钮允许加载预装有此行数据的userform 问题是,当加载表单时,初始化宏会将所有表单字段重置为“”,我还不知道如何告诉VBA加载调用行的数据 有什么关于如何进行的建议吗 以下是我目前掌握的代码: 单击“新建条目”按钮时调用userform Sub call_userform() Details.Show End Sub
以下是我目前掌握的代码: 单击“新建条目”按钮时调用userform
Sub call_userform()
Details.Show
End Sub
Private Sub btn_save_Click()
Dim emptyRow As Long
'Activate Data sheet
Sheets("Data").Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("B:B")) + 1
'Transfer information
Cells(emptyRow, 2).Value = serial.Value
Cells(emptyRow, 4).Value = created_on.Value
Cells(emptyRow, 5).Value = created_by.Value
Cells(emptyRow, 6).Value = priority.Value
Cells(emptyRow, 7).Value = floor.Value
Cells(emptyRow, 8).Value = area.Value
Cells(emptyRow, 9).Value = subarea.Value
Cells(emptyRow, 10).Value = details.Value
If fu_1.Value = True Then
Cells(emptyRow, 11).Value = fu_1.Caption
End If
If fu_2.Value = True Then
Cells(emptyRow, 11).Value = fu_2.Caption
End If
If fu_3.Value = True Then
Cells(emptyRow, 11).Value = fu_3.Caption
End If
If fu_4.Value = True Then
Cells(emptyRow, 11).Value = fu_4.Caption
End If
If fu_name.Value > 0 Or fu_department.Value > 0 Then
Cells(emptyRow, 12).Value = fu_name.Value & " " & fu_department.Value
End If
Cells(emptyRow, 13).Value = status.Value
End Sub
初始化userform时:
Private Sub UserForm_Initialize()
IC_logo.BackColor = RGB(81, 81, 73) ' ash grey
'Empty all fields
status.Value = "Open"
serial = Evaluate("randbetween(10000,30000)")
priority.Value = ""
created_on.Value = Format(Date, "dd/mm/yyyy")
created_by.Value = ""
department.Value = ""
floor.Value = ""
area.Value = ""
subarea.Value = ""
details.Value = ""
fu_name.Value = ""
fu_department = ""
Me.status.RowSource = "lst_status" 'Fill Status
Me.priority.RowSource = "lst_priority" 'Fill Priorities
created_by = Sheets("Settings").Range("B24") 'Fill Created By with Logon Username
department = Sheets("Settings").Range("B25") 'Fill Created By with Logon Department
Me.floor.RowSource = "lst_floor" 'Fill Floor
Me.area.RowSource = "lst_area" 'Fill Area
Me.subarea.RowSource = "lst_subarea" 'Fill Subarea
'Set follow up to construction company as per default
'fu_2.Value = True
'Set Focus on NameTextBox
priority.SetFocus
End Sub
单击“保存”按钮时
Sub call_userform()
Details.Show
End Sub
Private Sub btn_save_Click()
Dim emptyRow As Long
'Activate Data sheet
Sheets("Data").Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("B:B")) + 1
'Transfer information
Cells(emptyRow, 2).Value = serial.Value
Cells(emptyRow, 4).Value = created_on.Value
Cells(emptyRow, 5).Value = created_by.Value
Cells(emptyRow, 6).Value = priority.Value
Cells(emptyRow, 7).Value = floor.Value
Cells(emptyRow, 8).Value = area.Value
Cells(emptyRow, 9).Value = subarea.Value
Cells(emptyRow, 10).Value = details.Value
If fu_1.Value = True Then
Cells(emptyRow, 11).Value = fu_1.Caption
End If
If fu_2.Value = True Then
Cells(emptyRow, 11).Value = fu_2.Caption
End If
If fu_3.Value = True Then
Cells(emptyRow, 11).Value = fu_3.Caption
End If
If fu_4.Value = True Then
Cells(emptyRow, 11).Value = fu_4.Caption
End If
If fu_name.Value > 0 Or fu_department.Value > 0 Then
Cells(emptyRow, 12).Value = fu_name.Value & " " & fu_department.Value
End If
Cells(emptyRow, 13).Value = status.Value
End Sub
如前所述,现在的问题是如何使用当前行的数据加载userform?如果您的工作表上有一个按钮可以调用表单,则只需使用以下内容:
me.TextBox1.value = Cells(ActiveCell.Row, 2).Value 'ActiveCell.Row returns the row number of the current cell
因此,该按钮的唯一命令是
UserForm1。Show
您可以编写一个子类loadDataIntoForm(rowNum等长)
,根据自己的喜好填充表单的字段,并将其放入userform的代码中。
e、 g
然后,如果单击编辑按钮,则执行
Details.loadDataIntoForm(rowNum) 'where rowNum is something like ActiveCell.Row. This calls UserForm_Initialize and THEN loadDataIntoForm
Details.Show 'UserForm_Initialize won't get executed again
UserForm\u Initialize
在loadDataIntoForm
之前执行,因此字段首先由UserForm\u Initialize
函数清除,然后由loadDataIntoForm
重新填充。当然,您也可以先加载表单
Load Details
Details.loadDataIntoForm(ActiveCell.Row)
Details.Show
因此,您可以确保在loadDataIntoForm
之后未调用UserForm\u Initialize
当您可以使用F8逐行查看代码时,了解调用的内容
编辑:更好的方法可能是编写两个sub,一个用于初始化表单进行编辑(填充字段),另一个用于初始化表单进行新记录(清除字段) 它可能看起来像
Public Sub ShowNew()
'clear all the controls
me.Show
End Sub
Public Sub ShowEdit(rowNum As Long)
'populate the controls
me.Show
End Sub
然后,不要使用Details.Show
使用Details.ShowNew
和Details.ShowNew(rowNum)
我将如下所示:
- 使用
对象的UserForm
属性来存储一个“调用参数”,该参数将告诉UserForm是运行Tag
Sub还是运行InitializeValues()
oneFillValues()
- 使用
事件处理程序让UserForm决定要执行的操作UserForm\u Activate
Edit()
sub附加到工作表的“编辑”按钮,则前者将是
Sub Edit()
With UserForm3
.Tag = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row '<~~ tell the UserForm there's something to bring in so that it'll fill controls from the sheet instead of initializing them
.Show
.Tag = "" '<~~ bring Tag property back to its "null" value
End With
Unload UserForm3
End Sub
子编辑()
使用UserForm3
.Tag=ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row'您只有一个按钮吗?你能把你目前掌握的密码发出去吗?如果要在打开表单时加载文本框,请使用UserForm\u Activate()
子项“加载”代码与“保存”代码相反……是的,但我如何称呼它?如果表单是通过userform.show
打开的,那么它将自动在sub-userform\u initialize….中加载代码。。。。如何加载空表单并填充内容?我不会使用userform\u initialize加载数据,而是使用userform\u activate。让我澄清一下。当我使用userform.show
时,存储在sub-userform\u initialize中的代码会自动执行。我如何调用userform来触发userform\u activate sub?那会是userform.activate
?对我来说,当我用userform.show打开表单时,userform\u activate子被触发。那么userform\u初始化是如何触发的呢?分别地,我如何告诉VBA在单击“新建条目”按钮时加载一个空表单,而在单击活动行中的“编辑”按钮时加载一个已填充的表单?对不起,您完全弄丢了我。当单击行中的按钮时,我应该在哪里放置什么以及调用哪个子按钮?为了澄清,我在工作表上有两个按钮。一个是创建一个“新条目”,这意味着我需要在没有任何值的情况下加载表单,正如我在userform_initialize sub中定义的那样,然后是另一个“EDIT ENTRY”按钮,我希望表单加载活动行中的值。我该如何做到这一点?我应该使用什么subs?@Armitage2k我编辑了我的文章,以使其更清晰(希望如此)