这是我写的代码,就差一点点了就是调试不好,请大家帮我看看.For i = 1 To 20
a = Sheet1.Cells(i, 1)
b = Sheet1.Cells(i, 2)
c = Sheet1.Cells((i + 1), 1)
d = Sheet1.Cells((i + 1), 2)
l = Sheet1.Cells((i + 2), 1)
m = Sheet1.Cells((i + 2), 2)
If Not a = c Then
If Not l = c Then '如果需要比较的数字不相等时
Sheet1.Cells(i, 5) = b '输出结果
End If
Else
If i = 1 Then '如果是第一行时,那么。。
b = b + d
For j = (i + 1) To (i + 10)
e = Sheet1.Cells(j, 1)
f = Sheet1.Cells(j, 2)
g = Sheet1.Cells((j + 1), 1)
h = Sheet1.Cells((j + 1), 2)
If e = g Then
b = b + h
Sheet1.Cells(i, 5) = b
Else
Sheet1.Cells(i, 5) = b
End If
Next j
Else '如果不是第一行则执行下面。
k = Sheet1.Cells((i - 1), 1)
If k = a Then '如果下列不等于当前列时
Sheet1.Cells(i, 5) = "" '输出为空
Else
b = b + d '当前列数据+下列数据
For j = (i + 1) To (i + 10) '执行循环查看多少列相等数据
e = Sheet1.Cells(j, 1)
f = Sheet1.Cells(j, 2)
g = Sheet1.Cells((j + 1), 1)
h = Sheet1.Cells((j + 1), 2)
If e = g Then '如果相等数据相加
b = b + h
Sheet1.Cells(i, 5) = b
Else ' 如果不等直接输出数据
Sheet1.Cells(i, 5) = b
End If
Next j
End If
End If
End If
Next i
a = Sheet1.Cells(i, 1)
b = Sheet1.Cells(i, 2)
c = Sheet1.Cells((i + 1), 1)
d = Sheet1.Cells((i + 1), 2)
l = Sheet1.Cells((i + 2), 1)
m = Sheet1.Cells((i + 2), 2)
If Not a = c Then
If Not l = c Then '如果需要比较的数字不相等时
Sheet1.Cells(i, 5) = b '输出结果
End If
Else
If i = 1 Then '如果是第一行时,那么。。
b = b + d
For j = (i + 1) To (i + 10)
e = Sheet1.Cells(j, 1)
f = Sheet1.Cells(j, 2)
g = Sheet1.Cells((j + 1), 1)
h = Sheet1.Cells((j + 1), 2)
If e = g Then
b = b + h
Sheet1.Cells(i, 5) = b
Else
Sheet1.Cells(i, 5) = b
End If
Next j
Else '如果不是第一行则执行下面。
k = Sheet1.Cells((i - 1), 1)
If k = a Then '如果下列不等于当前列时
Sheet1.Cells(i, 5) = "" '输出为空
Else
b = b + d '当前列数据+下列数据
For j = (i + 1) To (i + 10) '执行循环查看多少列相等数据
e = Sheet1.Cells(j, 1)
f = Sheet1.Cells(j, 2)
g = Sheet1.Cells((j + 1), 1)
h = Sheet1.Cells((j + 1), 2)
If e = g Then '如果相等数据相加
b = b + h
Sheet1.Cells(i, 5) = b
Else ' 如果不等直接输出数据
Sheet1.Cells(i, 5) = b
End If
Next j
End If
End If
End If
Next i
Dim a(20) '定义一个数组,初始全为0
For i = 1 To 20
If Sheet1.Cells(i + 1, 1) <> Sheet1.Cells(i, 1) Then
a(i + 1) = i + 1
End If
Next i
'上面的for循环依次判断相临两行是否相等,不相等的话则将下一行的行号赋给数组
k = 1
For i = 1 To 20
If a(i) <> 0 Then
For j = k To a(i) - 1
m = m + Sheet1.Cells(j, 2)
Next j
Sheet1.Cells(k, 5) = m
k = a(i)
m = 0
End If
Next i