使用当前行

时间:2016-06-01 05:33:35

标签: excel vba excel-vba userform

我有一个带有多个文本框,单选按钮,下拉列表等的用户表单。当用户创建新条目时,数据将保存在数据表中,其中一条记录占据一行。现在我希望能够单击A列中的“编辑”按钮,该按钮允许加载预加载此行数据的用户表单。

问题在于,当加载表单时,初始化宏将所有表单字段重置为“”,而我还没弄明白如何告诉VBA加载调用行的数据。

有关如何解决这个问题的任何建议吗?

这是我到目前为止的代码: 单击NEW ENTRY按钮时调用userform

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

单击SAVE按钮时

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?这仍然是通过details.show吗?

3 个答案:

答案 0 :(得分:2)

我是这样的:

  • 使用Tag对象的UserForm属性来存储"调用参数"这将告诉UserForm是运行InitializeValues() Sub还是FillValues()

  • 使用UserForm_Activate事件处理程序让UserForm决定采取哪种操作

所以,假设您在工作表中添加了Edit()子"编辑"按钮,前者将是

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

答案 1 :(得分:0)

如果您的工作表上有一个按钮来调用表单,那么只需使用以下内容:

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

因此按钮的唯一命令是UserForm1.Show

答案 2 :(得分:0)

您可以编写类似loadDataIntoForm(rowNum As Long)的子类,根据您的喜好填充表单的字段,并将其放入用户表单的代码中。 e.g。

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

然后,如果单击编辑按钮,则执行

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_Initialize之前执行

loadDataIntoForm,因此UserForm_Initialize函数首先清除字段,然后由loadDataIntoForm重新填充。确保您还可以先加载表单

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

因此,您可以确定UserForm_Initialize之后未调用loadDataIntoForm

通过使用F8逐行完成代码,了解所调用的内容。

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

它可能看起来像

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.ShowDetails.ShowNew

而不是Details.ShowNew(rowNum)