【问题标题】:Excel Sum across multiple sheets跨多个工作表的 Excel 总和
【发布时间】:2020-08-08 20:33:42
【问题描述】:

我是 VBA 新手,希望有人可以帮助我。我创建了一个自定义函数来汇总多个工作表中单元格的值。但是,我必须使用该函数的工作表中的单元格与我需要求和的单元格不同。例如,我需要对所有工作表中的 B2 求和,但我需要使用该函数的地方是单元格 C2。有人可以帮忙吗? 我的代码:

Function AutoSum() As Variant
    AutoSum = 0
    For Each ws In Worksheets
        If Not ws Is Application.ThisCell.Parent Then AutoSum = AutoSum + ws.Range(Application.ThisCell.Address)
    Next
End Function

【问题讨论】:

  • 将要用作参数的单元格地址添加到函数中。

标签: excel vba


【解决方案1】:

你可以稍微改变它并放置你想要求和的范围

=autosum(d1) 将对所有 D1 单元格求和。

Function AutoSum(rng As Range) As Variant
    AutoSum = 0
    Application.Volatile True
    For Each ws In Worksheets
        If Not ws Is Application.ThisCell.Parent Then
            AutoSum = AutoSum + ws.Range(rng.Address)
        End If
    Next
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    相关资源
    最近更新 更多