宏代码如下
Sub 求和()
Dim mg As Range
Dim i As Long, k As Long, l As Long, m As Long
On Error Resume Next
Application.DisplayAlerts = False
k = 2 ' 单元格起始
l = 2 ' 单元格步长
i = 1
m = InputBox("求和的总行数", , 20)
Do While k <= m '当k>m时结束do
Do While Cells(l, 1) <> ""
l = l + 1 '当cells(l,1)不为空,l增加1,
Loop
If Cells(l - 1, 1) <> "" Then '当cells(l,1)=空 ,l-1 为空的单元格上边非空单元格的个数
Range(Cells(k, 1), Cells(l - 1, 1)).Select
Cells(l - 1, 2) = WorksheetFunction.Sum(Selection)
Cells(l - 1, 3) = Selection.Count
End If
k = l + 1
l = l + 1
Loop
End Sub
Dim i, rowcount As Long
Dim numcount As Long
Dim numsum As Double
Dim cellsvalue As Variant
'A列最大行数,A列是数据源,如果是B列怎替代A
rowcount = Sheet1.Range("A65536").End(xlUp).Row
'清空计算值
Range("B2:C" & rowcount).ClearContents
If rowcount > 1 Then
For i = 2 To (rowcount + 1)
cellsvalue = Sheet1.Range("A" & i).Value
'是否为空
If IsNull(cellsvalue) Or cellsvalue = "" Then
'写入结果
Sheet1.Range("C" & i - 1) = numcount
Sheet1.Range("B" & i - 1) = numsum
'重置计数器
numcount = 0
numsum = 0
ElseIf IsNumeric(cellsvalue) Then
'是否是数字
numcount = numcount + 1
numsum = numsum + CDbl(cellsvalue)
Else
MsgBox ("存在非数值数据")
Exit Sub
End If
Next
End If
这是一个浮点错误造成的!属于正常“不正常的”!
您可以修复一点点:
功能BTA(VALD由于双人间)
BTA =修复((VALD - 修复(VALD)+ 1E-16)* 60)
结束功能
因为上次使用修复功能,所以+1的E-16,你进入一定精度的自变量,结果不会影响!
类似的错误浮点计算,你可以搜索一下:“Excel的计算错误”
刚做的,希望采纳,点“统计”即可自动统计前600行数据。