再次完善了一下,详见代码
- Sub ssjss() '2023.5.30
- Dim rng As Range, rng1 As Range, rng2 As Range, rng3 As Range
- Dim r%, r1%, i%, j%
- With Sheets("原始数据")
- r = .[B65536].End(3).Row
- Set rng = .Range("s2:s" & r) '欲求平均值的列
- Set rng1 = .Range("C2:C" & r) '所属月份列
- Set rng2 = .Range("R2:R" & r) '进度固定:验收交工
- Set rng3 = .Range("J2:J" & r) '项目经理列
- For i = 2 To r: .Cells(i, "QQ") = Month(.Cells(i, "P")): Next
- Set rng4 = .Range("QQ2:QQ" & r) '俊工日期列/辅助列
- End With
- With Sheets("人工明细")
- r1 = .[A65536].End(3).Row - 1
- .Range("B5:Q" & r1) = ""
- If .[G2] = "" Then .[G2] = ">0"
- If .[I2] = "" Then .[I2] = ">0"
- For i = 5 To r1
- On Error Resume Next
- For j = 3 To 14
- .Cells(i, j) = WorksheetFunction.AverageIfs(rng.Offset(0, i - 5), _
- rng1, .[G2], rng2, .[k2], rng3, .Cells(4, j), rng4, .[I2])
- Next j
- .Cells(i, 2) = WorksheetFunction.AverageIfs(rng.Offset(0, i - 5), _
- rng1, .[G2], rng2, .[k2], rng4, .[I2])
- Next i
- 地址 = .Range("B5:B" & r1).Address(0, 0)
- .Cells(r1 + 1, 2).Resize(1, j - 2) = "=sum(" & 地址 & ")"
- End With
- End Sub
- Sub Button1_Click()
- Call ssjss '
- End Sub
复制代码 |