enter image description here我有两个Excel工作表(“记录”)和(“注册”),“注册”是数据库。从这个数据库中,我需要根据每个员工的员工ID(单元格值)创建记录。我正在搜索应该为我提供培训的VBA代码。一旦我在单元格中输入了他们的ID,然后单击“命令按钮”,就记录每个雇员。随附Excel屏幕供参考。
步骤1:在“记录”表中输入员工ID 步骤2:在记录表中单击命令按钮“过滤器” 步骤3:运行和过滤“注册”中的数据并填写“记录”的VBA代码。
如果我在“记录”表中键入另一个员工ID,则它应清除上一个查询的Contents。并产生数据。
请帮助我,我在VBA中表现不好。 >
Sub Button2_Click()
'Declare the variables
Dim RegisterSh As Worksheet
Dim RecordSh As Worksheet
Dim EmployeeRange As Range
Dim rCell As Range
Dim i As Long
'Set the variables
Set RegisterSh = ThisWorkbook.Sheets("Register")
Set RecordSh = ThisWorkbook.Sheets("Record")
'Clear old data Record Sheet
RecordSh.Range("A8:F107").ClearContents
Set EmployeeRange = RegisterSh.Range(RegisterSh.Cells(6, 4), RegisterSh.Cells(Rows.Count, 6).End(xlUp))
'I went from the cell row6/column4 (or D6) and go down until the last non empty cell
i = 7
For Each rCell In EmployeeRange 'loop through each cell in the range
If rCell = RecordSh.Cells(4, 2) Then 'check if the cell is equal to "Record"
i = i + 1 'Row number (+1 everytime I found another "Record")
RecordSh.Cells(i, 1) = i - 7 'S No.
RecordSh.Cells(i, 2) = rCell.Offset(0, 2) 'Training name
RecordSh.Cells(i, 3) = rCell.Offset(0, -2) 'End date
RecordSh.Cells(i, 4) = rCell.Offset(0, 6) 'Validity
RecordSh.Cells(i, 5) = rCell.Offset(0, 7) 'Remarks
RecordSh.Cells(i, 6) = rCell.Offset(0, 5) 'Minimal requirement
End If
Next rCell
End Sub
答案 0 :(得分:1)
您的代码缺少一些您可能需要研究的基本部分:
我认为,如果您熟悉Find函数,则可以轻松地自己完成此宏。这是一个很好的指南:https://excelmacromastery.com/excel-vba-find
祝你好运!