我有一个excel文件,其中我有120列和400行。我想总结120列的所有行并获得最大值的Max。 如何在不使用数组公式的情况下实现它
A B C
1 2 3
2 3 5
3 4 7
4 5 9
5 8 13
4 2 6
在这个例子中,我应该得到答案为43,因为C列的总和最大
答案 0 :(得分:1)
这是一个VBA解决方案,它返回基于1的二维数组中的最大行数或最大列数:
Function MaxSum(A As Variant, Optional ByRows = True) As Double
'returns the maximum sum of the rows (default) or
'columns of a 1-based 2-dimensional array
Dim sums As Variant
Dim i As Long, j As Long, m As Long, n As Long
Dim sum As Double
If TypeName(A) = "Range" Then A = A.Value
m = UBound(A, 1)
n = UBound(A, 2)
If ByRows Then
ReDim sums(1 To m)
For i = 1 To m
sum = 0
For j = 1 To n
sum = sum + A(i, j)
Next j
sums(i) = sum
Next i
Else
ReDim sums(1 To n)
For i = 1 To n
sum = 0
For j = 1 To m
sum = sum + A(j, i)
Next j
sums(i) = sum
Next i
End If
MaxSum = Application.WorksheetFunction.Max(sums)
End Function
根据您的示例数据,=MaxSum(A1:C6)
评估为26,=MaxSum(A1:C6, False)
评估为43。