|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
'稍作注释
Option Explicit
Sub 求和()
Dim arr, i, j, a, b
arr = [a1].CurrentRegion
arr = [a1].Resize(UBound(arr, 1) + 1, UBound(arr, 2) + 1) '增加行、列各1,需要写入行、列和值
For j = 2 To UBound(arr, 2) - 1 '头尾列不作处理
For i = 2 To UBound(arr, 1) - 1 '头尾行不作处理
arr(UBound(arr, 1), j) = arr(UBound(arr, 1), j) + arr(i, j) '列求和
arr(i, UBound(arr, 2)) = arr(i, UBound(arr, 2)) + arr(i, j) '行求和
Next i, j
a = 1: b = 1: arr(UBound(arr, 1), UBound(arr, 2)) = 1 '标志为非0,写代码简单一点,无它用
For i = 2 To UBound(arr, 1) '去除不满足条件的行
If arr(i, UBound(arr, 2)) <> 0 Then
a = a + 1
For j = 1 To UBound(arr, 2): arr(a, j) = arr(i, j): Next
End If
Next
For j = 2 To UBound(arr, 2) '去除不满足条件的列
If arr(UBound(arr, 1), j) <> 0 Then
b = b + 1
For i = 1 To UBound(arr, 1): arr(i, b) = arr(i, j): Next
End If
Next
arr(a, 1) = "合计": arr(1, b) = "合计": arr(a, b) = vbNullString '去除标志
[a1].Resize(a, b) = arr
End Sub |
|