计算选中区域内的平均分,及格率,优秀率

写入 workbook 的 SheetSelectionChange 事件中可以在多个工作表之间共用

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  If (Target.Columns.Count = 1) And (Target.Rows.Count > 1) Then
    icount = Application.WorksheetFunction.CountA(Target)
    iSum = Application.WorksheetFunction.Sum(Target)
    iAvg = Application.WorksheetFunction.Average(Target)
    i1 = Application.WorksheetFunction.CountIf(Target, ">=85")
    i2 = Application.WorksheetFunction.CountIf(Target, ">=60")
    strMsg = icount & " / " & iSum & " / " & Format(iAvg, "0.00") & "  " & i1 & "[" & Format(i1 / icount, "0.00%") & "]   " & i2 & "[" & Format(i2 / icount, "0.00%") & "]"
  
    '计算结果写入指定工作表并复制到剪贴板
    Sheet5.Cells(1, 2).Value = iSum
    Sheet5.Cells(1, 3).Value = iAvg
    Sheet5.Cells(1, 4).Value = i1
    Sheet5.Cells(1, 5).Value = Format(i1 / icount, "0.00%")
    Sheet5.Cells(1, 6).Value = i2
    Sheet5.Cells(1, 7).Value = Format(i2 / icount, "0.00%")
    Sheet5.Range("b1:g1").Copy
  
    MsgBox (strMsg)
  End If
End Sub
posted @ 2022-07-14 15:17  汉学  阅读(101)  评论(0)    收藏  举报