3D Sum如果使用List来循环所有需要的最终修复

我有以下cell AI1表摘要中的所有工作表名称列表,然后删除顶部3,从AI4select底部并命名范围“发票”

我正在试图用这个在所有表单上执行sumif公式。 它可以手工粘贴到一个单元格中,但是在VBA中,从'"&Invoices&"'!$A$2006:$A$3005"),$A3,INDIRECT("'"&Invoices&"'!B$2006:B$3005")))"

起初,它正在阅读它作为评论(绿色)

 For i = 1 To Sheets.Count Sheets("Summary").Range("AI1")(i, 1).Value = Sheets(i).Name Next i Sheets("Summary").Select Sheets("Summary").Range("AI1:AI3").Clear Sheets("Summary").Activate Sheets("summary").Range(ActiveSheet.Range("AI4"), ActiveSheet.Range("AI4").End(xlDown)).Select Selection.Name = "Invoices" Sheets("summary").Range("B3").Formula = "=SUMPRODUCT(SUMIF(INDIRECT("'"&Invoices&"'!$A$2006:$A$3005"),$A3,INDIRECT("'"&Invoices&"'!B$2006:B$3005")))" 

为了扩大我的评论:VBA认为“是string的末尾,所以如果你想要它实际上被视为一个字符,你需要将它转义为”“你的代码应该是:

 Sheets("summary").Range("B3").Formula = "=SUMPRODUCT(SUMIF(INDIRECT(""'""&Invoices&""'!$A$2006:$A$3005""),$A3,INDIRECT(""'""&Invoices&""'!B$2006:B$3005"")))" 

在有很多字符的时候检查这个简单的方法就是把它写在即时窗口中。 我放

 ? "=SUMPRODUCT(SUMIF(INDIRECT(""'""&Invoices&""'!$A$2006:$A$3005""),$A3,INDIRECT(""'""&Invoices&""'!B$2006:B$3005"")))" 

以确认它。

谢谢,对于任何需要这个的人来说,这是完整的

  *****'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, eg 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"")))"