sumifs 循环所有工作表

问题描述 投票:0回答:1

无论单元格引用如何,我如何在 VBA 中跨多个工作表进行求和公式引用?

这是构建在模板中的,每次运行时都会有不同数量的具有不同名称的工作表,因此我无法引用这些工作表。

vba excel sumifs
1个回答
0
投票

这就是完整的完成方式:

我原来的公式是

"=SUMPRODUCT(SUMIF(INDIRECT(" '"&Invoices&"'!"&"A2006:A3005"),A3,INDIRECT("'"&Invoices&"'!"&"B2006:B3005")))" 

这在直接放入单元格时有效,但正如您所看到的,当将其添加到 VBA 时,它会将其读取为注释。要解决此问题,每次使用

"
时,您都需要添加额外的
"
,如下所示(除了公式开头的
"=
之前和公式末尾的
)"
之后)。

 *****'list all the sheet names in cell AI1 of the sheet summary*****
For i = 1 To Sheets.Count
Sheets("Summary").Range("AI1")(i, 1).Value = Sheets(i).Name
Next i
***'clear the first 3 entries in AI as i didnt need the first three sheet names***
Sheets("Summary").Select
Sheets("Summary").Range("AI1:AI3").Clear
***'select the first sheet name, which is in AI4 as we cleard the first 3 to the last used cell, e.g Ctrl.Shift.down*** 
Sheets("Summary").Activate
Sheets("summary").Range(ActiveSheet.Range("AI4"),    ActiveSheet.Range("AI4").End(xlDown)).Select
***' Name the range invoices***
Selection.Name = "Invoices"
' ***Formula to do a sumIf looping all the shets in the named range Invoices***
Sheets("summary").Range("B3").Formula = "=SUMPRODUCT(SUMIF(INDIRECT(""'""&Invoices&""'!$A$2006:$A$3005""),$A3,INDIRECT(""'""&Invoices&""'!B$2006:B$3005"")))"
© www.soinside.com 2019 - 2024. All rights reserved.