我正在使用excel 2010.
我有一张名为“LOG”的工作表,其中包含所有员工的日志,如下所示:
现在我有另一张名为sheet1
的表格
我想要的是一个应该提示员工ID和月份的宏,然后显示该员工在该特定月份的所有日志。
我尝试过很多东西但是做不到。
答案 0 :(得分:0)
以下代码将在RESULTS_COLUMN
常量值指定的列中列出结果,只需将以下代码添加到工作表中即可。您还需要3个命名范围 - EmployeeCode
,StartDate
和EndDate
- 来输入您的条件,但您可以随时提示这些方式:
Sub DisplayRecords()
Const RESULTS_COLUMN As Long = 5
Dim strEmployeeCode As String
Dim dtStartDate As Date, dtEndDate As Date
Dim c As Range
On Error GoTo ErrorTrap
strEmployeeCode = [EmployeeCode]
dtStartDate = [StartDate]
dtEndDate = [EndDate]
' Clear the results column
With Sheet1
.Columns(RESULTS_COLUMN).ClearContents
.Cells(1, RESULTS_COLUMN).Value = "Results"
.Cells(1, RESULTS_COLUMN).Font.Bold = True
End With
For Each c In Sheet1.UsedRange.Columns(1).Cells
If c.Value = strEmployeeCode And c.Offset(, 1) >= dtStartDate And c.Offset(, 1) <= dtEndDate Then
' Find the next available cell in results column
Sheet1.Cells(Sheet1.Rows.Count, RESULTS_COLUMN).End(xlUp).Offset(1).Value = c.Offset(, 1).Value
End If
Next c
Exit Sub
ErrorTrap:
MsgBox "An error has occurred, please check input criteria", vbExclamation, ThisWorkbook.Name
End Sub