다른 사람들처럼 피벗 테이블이 가장 좋습니다. 피벗 테이블을 사용하는 방법이나 유용한 정보가 확실하지 않은 경우 refer to this SO post where I explain in detail
어쨌든, 시작하기 쉽도록 아래의 VBA 함수를 조합했습니다. 결코 가장 효율적인 방법은 아닙니다. 또한 다음과 같은 가정한다 :
Sheet 1
는
A
는
B
이 시간
C
을 가지고 총 시간을 위해 예약되어 직원 ID가 모든 데이터가를
D
에 사용할 수 있습니다 처리 상태 출력
물론이 코드는 코드를 약간 변경하여 매우 쉽게 변경할 수 있습니다. 코드를 검토하면 이해할 수있는 의견이 있습니다.
Status
열이 있어야하는 이유는 이미 처리 된 Staff Id
을 처리하지 않기 위해서입니다. 이 열의 필요성을 피하기 위해 코드를 매우 바꿀 수는 있지만, 이것이 내가 사물을 다루는 방법입니다.
CODE
Public Sub HoursForEmployeeById()
Dim currentStaffId As String
Dim totalHours As Double
Dim totalStaffRows As Integer
Dim currentStaffRow As Integer
Dim totalSearchRows As Integer
Dim currentSearchRow As Integer
Dim staffColumn As Integer
Dim hoursColumn As Integer
Dim totalHoursColumn As Integer
Dim statusColumn As Integer
'change these to appropriate columns
staffColumn = 1
hoursColumn = 2
totalHoursColumn = 3
statusColumn = 4
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
totalStaffRows = Sheet1.Cells(Rows.Count, staffColumn).End(xlUp).Row
For currentStaffRow = 2 To totalStaffRows
currentStaffId = Cells(currentStaffRow, staffColumn).Value
'if the current staff Id was not already processed (duplicate record)
If Not StrComp("Duplicate", Cells(currentStaffRow, statusColumn).Value, vbTextCompare) = 0 Then
'get this rows total hours
totalHours = CDbl(Cells(currentStaffRow, hoursColumn).Value)
'search all subsequent rows for duplicates
totalSearchRows = totalStaffRows - currentStaffRow + 1
For currentSearchRow = currentStaffRow + 1 To totalSearchRows
If StrComp(currentStaffId, Cells(currentSearchRow, staffColumn), vbTextCompare) = 0 Then
'duplicate found: log the hours worked, set them to 0, then mark as Duplicate
totalHours = totalHours + CDbl(Cells(currentSearchRow, hoursColumn).Value)
Cells(currentSearchRow, hoursColumn).Value = 0
Cells(currentSearchRow, statusColumn).Value = "Duplicate"
End If
Next
'output total hours worked and mark as Processed
Cells(currentStaffRow, totalHoursColumn).Value = totalHours
Cells(currentStaffRow, statusColumn).Value = "Processed"
totalHours = 0 'reset total hours worked
End If
Next
Application.ScreenUpdating = False
Application.Calculation = xlCalculationAutomatic
End Sub
BEFORE
AFTER
출처
2013-03-14 17:38:38
Sam
아무 것도 시도하지 않았습니까 ?? 피벗 테이블이 문제를 해결할만큼 충분히 좋을 수 있기 때문에 VBA로 수행해야합니까 –
실제로 피벗 테이블을 사용하면 문제가 해결 될 것입니다 ... –
정확하게 피벗 테이블을 사용하여 데이터를 그룹화하고 합계하십시오. –