使用当前行的logging填充用户窗体

我有一个用户窗体,有几个文本框,单选button,下拉菜单等。当用户创build一个新的条目时,数据保存在数据表中,一条logging占据一行。 现在我希望能够点击列A中的“编辑”button,它允许加载预加载该行数据的用户表单。

问题是,当表单被加载时,初始化macros将所有表单字段重置为“”,我还没有想出如何告诉VBA加载调用行的数据。

有关如何去做这个的任何build议?


这里是我迄今为止的代码:当点击NEW ENTRYbutton时调用用户表单

Sub call_userform() Details.Show End Sub 

当用户窗体初始化时:

 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 

当点击SAVEbutton时

 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 

如前所述,现在的问题是如何加载用户窗体与当前行的数据? 这仍然是通过details.show

我会去如下:

  • 使用UserForm对象的Tag属性来存储一个“调用参数”,它将告诉UserForm是运行一个InitializeValues() Sub还是一个FillValues()

  • 使用UserForm_Activate事件处理程序具有UserForm决定要采取的操作

所以,假设你附加一个Edit()子到你的工作表“编辑”button,前者是

 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 

然后在你的UserForm代码窗格中放置这个

 Private Sub UserForm_Activate() If Me.Tag = "" Then '<~~ if there's no info from Tag property... InitializeValues '<~~ ... then Initialize controls values Else FillValues '<~~ ...otherwise fill controls with sheet values End If End Sub Private Sub InitializeValues() With Me .ComboBox1.RowSource = "initRange" .serial.Value = "actText1" .created_on.Value = "actText2" .created_by.Value = "actText3" ' and so on... End With End Sub Private Sub FillValues() With Me .serial.Value = Cells(.Tag, 2).Value .created_on.Value = Cells(.Tag, 4).Value .created_by.Value = Cells(.Tag, 5).Value '.. and so on End With End Sub 

如果您的工作表上有一个button用于调用窗体,则只需使用如下所示的内容:

 me.TextBox1.value = Cells(ActiveCell.Row, 2).Value 'ActiveCell.Row returns the row number of the current cell 

所以这个button唯一的命令是UserForm1.Show

你可以写一个类似loadDataIntoForm(rowNum As Long)的子表单,填充表单的字段并将其放入到用户表单的代码中。 例如

 Public Sub loadDataIntoForm(rowNum As Long) me.somecontrol.Value = Sheets("Data").cells(rowNum,1).Value '... End Sub 

然后如果编辑button被点击执行

 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_InitializeloadDataIntoForm被执行之前被执行,所以这些字段首先由UserForm_Initialize函数清除,然后由loadDataIntoForm重新填充。 可以肯定你也可以先载入表单

 Load Details Details.loadDataIntoForm(ActiveCell.Row) Details.Show 

所以你可以确定UserForm_Initialize不会在loadDataIntoForm之后调用。

当你可以使用F8一行一行地通过代码的时候,了解什么是被调用的。


编辑:一个更好的方法可能是编写两个子目录,一个用于初始化编辑窗体(填充字段),另一个用于初始化新logging的窗体(清除字段)

它可能看起来像

 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.ShowNewDetails.ShowNew(rowNum)