|
- Sub SumSalary()
- Dim ws As Worksheet
- Set ws = Worksheets("总表")
- Dim lrow As Long
- lrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
- Dim salaryCol As String
- salaryCol = "C"
- If ws.Range(salaryCol & "1").Value <> "1月工资" Then
- Select Case ws.Range(salaryCol & "1").Value
- Case "2月工资"
- salaryCol = "D"
- Case "3月工资"
- salaryCol = "E"
- End Select
- End If
- Dim lastRow As Long
- lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
- Dim rg As Range
- Dim deptName As String
- Dim jobTitle As String
- Dim salary As Double
- Dim i As Long
- If ws.Cells(1, "C").Value = "1月工资" Then
- ws.Range("C1").EntireColumn.Delete Shift:=xlToLeft
- End If
- ws.Range("C1").Value = salaryCol
- For i = 2 To lastRow
- deptName = ws.Cells(i, "A").Value
- jobTitle = ws.Cells(i, "B").Value
- salary = ws.Cells(i, salaryCol).Value
- Set rg = ws.Range("A2:C" & lrow).Find(what:=deptName, lookat:=xlWhole)
- Do Until rg Is Nothing
- If rg.Offset(0, 1).Value = jobTitle Then
- rg.Offset(0, salaryColRange(salaryCol) - 3).Value = rg.Offset(0, salaryColRange(salaryCol) - 3).Value + salary
- Exit Do
- Else
- Set rg = ws.Range("A" & rg.Row + 1 & ":C:C" & lrow).Find(what:=deptName, lookat:=xlWhole)
- End If
- Loop
- If rg Is Nothing Then
- lrow = lrow + 1
- ws.Range("A" & lrow).Value = deptName
- ws.Range("B" & lrow).Value = jobTitle
- ws.Range("C" & lrow).Value = 0
- ws.Range(salaryCol & lrow).Value = salary
- End If
- Next i
- End Sub
- Function salaryColRange(col As String) As Long
- salaryColRange = Asc(col) - 64
- End Function
复制代码 |
|