表格操作系列
五、对表格或数组进行行或列求和
在做报表时,经常会需要对取得的数据进行行或列的求和汇总操作,下面的代码可以完成对数组的行或列的求和操作,也适用于对表格的指定区域进行求和汇总。
函数代码
Public Function SumArrRow(Arr, 列, Optional WithArr As Boolean = True)
'默认带数组数据
Dim i As Integer
Dim j As Integer
Dim sp() As String
Dim dic As Object
Dim LB As Integer
Dim UB As Integer
Dim UB2 As Integer
Dim tmp()
Dim Rtmp()
LB = LBound(Arr)
UB = UBound(Arr)
UB2 = UBound(Arr, 2)
Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = vbTextCompare '采用文本的比较方式,不分大小写
For i = LB To UB
dic(Arr(i, LB)) = i '整理列字段的列数
Next
If WithArr Then
ReDim tmp(LB To UB, LB To UB2 + 1)
For i = LB To UB
For j =