Sub Statistics() Dim lastRow As Long Dim maxVal As Double Dim minVal As Double Dim avgVal As Double Dim countVal As Long Dim countRatio As Double lastRow = Cells(Rows.Count, "A").End(xlUp).Row '获取数据的最后一行 '计算最大值 maxVal = WorksheetFunction.Max(Range("A1:A" & lastRow)) Range("B1").Value = maxVal '计算最小值 minVal = WorksheetFunction.Min(Range("A1:A" & lastRow)) Range("B2").Value = minVal '计算平均值 avgVal = WorksheetFunction.Average(Range("A1:A" & lastRow)) Range("B3").Value = avgVal '计算区间计数 countVal = WorksheetFunction.CountIf(Range("A1:A" & lastRow), ">=60") Range("B4").Value = countVal '计算所占比例 countRatio = countVal / WorksheetFunction.CountA(Range("A1:A" & lastRow)) Range("B5").Value = countRatio End Sub,最后输出结果加中文
时间: 2023-12-29 18:02:35 浏览: 128
Sub 统计数据()
Dim lastRow As Long
Dim maxVal As Double
Dim minVal As Double
Dim avgVal As Double
Dim countVal As Long
Dim countRatio As Double
lastRow = Cells(Rows.Count, "A").End(xlUp).Row '获取数据的最后一行
'计算最大值
maxVal = WorksheetFunction.Max(Range("A1:A" & lastRow))
Range("B1").Value = "最大值:" & maxVal
'计算最小值
minVal = WorksheetFunction.Min(Range("A1:A" & lastRow))
Range("B2").Value = "最小值:" & minVal
'计算平均值
avgVal = WorksheetFunction.Average(Range("A1:A" & lastRow))
Range("B3").Value = "平均值:" & avgVal
'计算区间计数
countVal = WorksheetFunction.CountIf(Range("A1:A" & lastRow), ">=60")
Range("B4").Value = "大于等于60的数量:" & countVal
'计算所占比例
countRatio = countVal / WorksheetFunction.CountA(Range("A1:A" & lastRow))
Range("B5").Value = "大于等于60的数量占比:" & Format(countRatio, "0.00%")
End Sub
阅读全文