一个奇怪的VBA函数:在excel里计算对角线的和

  • Post author:
  • Post category:IT
  • Post comments:0评论

想要计算一个区域里对角线的和,但SUMIF里面的那个criteria实在太简陋了,只能用vba来实现,大概长这样:

引用
Function sum_diag(n As Integer, ParamArray args() As Variant) As Variant

    result = 0

    For i = LBound(args) To UBound(args)

        For Each elem In args(i)

            If elem.Row + elem.Column = n Then

                result = result + elem.Value

            End If

        Next elem

    Next i

    sum_diag = result

End Function

然后这么用:

引用
=sum_diag(ROW()+COLUMN(), $B$1:$D$3)

点击在新窗口中浏览此图片

发表回复