简体   繁体   English

如何通过excel中的工作表索引号对多个工作表中的相同单元格求和?

[英]How can you sum the same cell across multiple worksheets by worksheet index number in excel?

I am trying to sum cell Y116 across all valid worksheets in my excel workbook.我正在尝试对我的 excel 工作簿中所有有效工作表的单元格 Y116 求和。 To help define the valid worksheets, I wrote a VBA function, SHEETNAME(number), that returns the name of the worksheet at the given worksheet index (number).为了帮助定义有效的工作表,我编写了一个 VBA 函数 SHEETNAME(number),它返回给定工作表索引(编号)处的工作表名称。 I did this because the names and number of valid worksheets will never be constant, however the valid range will always start at the 3rd worksheet (ie SHEETNAME(3)) and will always end at the third from last worksheet (ie SHEETNAME(SHEETS()-2)) in my workbook.我这样做是因为有效工作表的名称和数量永远不会恒定,但是有效范围将始终从第 3 个工作表(即 SHEETNAME(3))开始,并始终以倒数第三个工作表结束(即 SHEETNAME(SHEETS( )-2)) 在我的工作簿中。

I feel like this should be relatively straightforward with both SUM() and INDIRECT(), but I keep getting reference errors (#REF!).我觉得这对于 SUM() 和 INDIRECT() 应该相对简单,但我不断收到参考错误(#REF!)。

I can get the string formatted how I want it with我可以用我想要的方式格式化字符串

="'"&SHEETNAME(3)&":"&SHEETNAME(SHEETS()-2)&"'!Y116"

but I get a reference error when I try to put it all together:但是当我尝试将它们放在一起时出现参考错误:

=SUM(INDIRECT("'"&SHEETNAME(3)&":"&SHEETNAME(SHEETS()-2)&"'!Y116"))

I know cell Y116 is a valid reference in all of my worksheets because I can hardcode the formula with the actual names of the worksheets instead of the index and I get the answer I am looking for.我知道单元格 Y116 是我所有工作表中的有效引用,因为我可以使用工作表的实际名称而不是索引对公式进行硬编码,并且我得到了我正在寻找的答案。 Any advice?有什么建议吗?

Here is the SHEETNAME() function: Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name End Function
这是 SHEETNAME() 函数: Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name End Function
Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name End Function

To do what you want with SUM(INDIRECT()), on needs to return an array of sheet names to the indirect.要使用 SUM(INDIRECT()) 执行您想要的操作,需要将工作表名称数组返回给间接对象。

To do this one will need to change the UDF to:为此,需要将 UDF 更改为:

Function SHEETNAME(srt As Long, ed As Long) As Variant
Application.Volatile

If ed - srt <= 0 Then Exit Function

Dim temp() As Variant
ReDim temp(1 To ed - srt + 1) As Variant

Dim i As Long
For i = srt To ed
    temp(i - srt + 1) = Worksheets(i).Name
Next

SHEETNAME = temp
End Function

Then one can use:然后可以使用:

=SUM(INDIRECT("'"&SHEETNAME(3,SHEETS()-2)&"'!Y116"))

But if you are going to iterate the sheets anyway, why not just do the sum in the UDF:但是,如果您无论如何都要迭代工作表,为什么不直接在 UDF 中进行求和:

Function MY3DSUM(srt As Long, ed As Long, add As String) As Double
Application.Volatile

If ed - srt <= 0 Then Exit Function

Dim temp As Double
temp = 0

Dim i As Long
For i = srt To ed
     temp = temp + Worksheets(i).Range(add).Value2
Next

MY3DSUM = temp
End Function

Then you would call it:然后你会称它为:

 =MY3DSUM(3,SHEETS()-2,"Y116")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 跨多个工作表的总和和索引匹配 - Sum and Index match across multiple worksheets 在不同数量的工作表中汇总同一个表格 - Sum the same table across different number of worksheets 如何根据另一个工作表上的单元格值在多个工作表中隐藏/显示行 - How to hide / show rows across multiple worksheets based on a cell value on another worksheet 如何在工作表为空白的工作表中的公式中保留Excel引用? - How to persist Excel References in formulas across worksheets where the worksheet is blank? 在excel中的多个工作表中锁定相同的单元格? - Lock same cells across multiple worksheet in excel? 如何参考带有 VBA 的单元格在 Excel 中选择工作表? - How can you select a worksheet in Excel with reference to a cell with VBA? Excel公式-修复多个工作表中的单元格引用 - Excel formula - fixing cell reference across multiple worksheets 引用 Excel 函数中的最后一个工作表以对多个工作表中的相同范围求和 - Reference to last worksheet in Excel Function to Sum same range across multiple sheets 带非特定工作表名称或工作表数量的多个工作表的单元格区域中替换零 - Replace Zeros in Cell Range for Multiple Worksheets w/Non-Specific Worksheet names or Number of Worksheets 如何以编程方式检索Excel工作表中的页数? - How can you programmatically retrieve the number of pages in an Excel worksheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM