countVal20 = WorksheetFunction.CountIf(Range("A2:A" & lastRow), ">=20") Range("c21").Value = "大于等于20的数量" Range("d21").Value = countVal20,改为统计总数量
时间: 2023-07-03 17:09:03 浏览: 83
可以将原来的代码修改为:
```
countVal = WorksheetFunction.CountA(Range("A2:A" & lastRow))
Range("C21").Value = "总数量"
Range("D21").Value = countVal
```
这样就能统计出总数量了。
相关问题
'计算区间计数 countVal = WorksheetFunction.CountIf(Range("A1:A" & lastRow), ">=20 and <40") Range("c2").Value = "大于等于20的数量:" & countVal countVal = WorksheetFunction.CountIf(Range("A1:A" & lastRow), ">=40 AND <60") Range("c3").Value = "大于等于40的数量:" & countVal countVal = WorksheetFunction.CountIf(Range("A1:A" & lastRow), ">=60") Range("c4").Value = "大于等于60的数量:" & countVal '计算所占比例 countRatio = countVal / WorksheetFunction.CountA(Range("A1:A" & lastRow)) Range("d2").Value = "大于等于20的数量占比:" & Format(countRatio, "0.00%") countRatio = countVal / WorksheetFunction.CountA(Range("A1:A" & lastRow)) Range("d3").Value = "大于等于40的数量占比:" & Format(countRatio, "0.00%") countRatio = countVal / WorksheetFunction.CountA(Range("A1:A" & lastRow)) Range("d4").Value = "大于等于60的数量占比:" & Format(countRatio, "0.00%")
这段代码使用了Excel VBA的WorksheetFunction函数来计算一个Excel表格中某一列的数值符合特定条件的数量,并计算出所占比例。具体来说,这段代码计算了表格中大于等于20、40、60的数量,以及它们所占的比例,然后将结果输出到表格中的C2、C3、C4、D2、D3、D4单元格中。其中,lastRow是一个变量,代表表格中数据的最后一行。
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,最后输出结果加中文
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
阅读全文