2012-12-28 116 views
-1

我使用Excel 2010中
我有一個名爲「日誌」表,其中有全體員工的這樣的記錄:Excel宏的員工記錄

Example Data

現在我有另一個表名爲sheet1
我想要的是一個應該提示員工ID和月份的宏,然後顯示該員工特定月份的所有日誌。

我已經嘗試了很多東西,但無法做到這一點。

+0

@idevelop ...謝謝你的提示。剛接受答案。 – aki003

回答

0

以下代碼將列出RESULTS_COLUMN常數值指定的列中的結果,只需將以下代碼添加到工作表中。您還需要3米命名範圍 - EmployeeCodeStartDateEndDate - 輸入你的標準,雖然你總是可以促使這些另一種方式:

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 
+0

感謝兄弟,但這個搜索月份將如何。我的意思是這將顯示該員工的所有日誌。我希望它能夠在該員工的特定月份展示。 – aki003

+0

對不起,錯過了那一點。相應地更新了我的答案 – CrazyHorse

+0

兄弟沒問題。如何將msgbox的所有內容放入新的工作表或工作表中 – aki003