无法添加重复的源参考VBA / Excel - 多个工作表的合并

时间:2016-08-25 23:21:54

标签: excel vba excel-vba

所有工作表都包含在同一工作簿中。 const wait5 = (something) => new Promise((resolve, reject) => { setTimeout(() => resolve(something + 5), 5000) }) const wait10 = (something) => new Promise((resolve, reject) => { setTimeout(() => resolve(something + 10), 10000) }) const doTest = (x, y, prep) => { describe('main', function() { let z; before(function (done) { this.timeout(30000); prep(y).then((val) => { z = val; done(); }) }) it('should return the right number for ' + x + ' and ' + y, () => { expect(x).to.equal(z) }) }) } [[17, 12, wait5], [15, 5, wait10], [15, 5, wait5]].forEach((listing) => doTest(...listing)) /* Result: main √ should return the right number for 17 and 12 main √ should return the right number for 15 and 5 main 1) should return the right number for 15 and 5 2 passing (20s) 1 failing 1) main should return the right number for 15 and 5: AssertionError: expected 15 to equal 10 + expected - actual -15 +10 */ 将成为合并的摘要表。 除Sheet1之外,还有动态数量的工作表。 我在Sheet1之后的每个源数据表中都有一组设定的数据:

Sheet1

此数据需要合并到摘要表(C54:H56 (a 3x6 grid of cells) )上的单个3x6区域。 我不是指复制数据,而是指Excel中的实际合并功能。

目前我收到的错误是:

  

运行时错误' 1004':无法添加重复的源参考

如上所述,我尝试了很多不同的变体,并且总是以语法,下标超出范围或当前问题结束。

当我检查Sheet1的内容时,正在捕获正确的工作表名称和范围。但是,我实际上无法将这些传递给合并功能本身和/或正确实现它们。

代码:

ConsolidationArray

1 个答案:

答案 0 :(得分:1)

ConsolidationArray需要基于零,并且范围引用字符串采用R1C1样式表示法。

Sub WorkseetParsingLoopToGetData()
     Dim i As Integer
     Dim WS_Count As Integer
     Dim ConsolidationArray()

     WS_Count = ActiveWorkbook.Worksheets.count 'last worksheet
     ReDim ConsolidationArray(WS_Count - 2) 'Array to be Sheet2 to last worksheet

     For i = 2 To WS_Count
          ConsolidationArray(i - 2) = Worksheets(i).Name & "!R54C3:R56C8" '"!C54:H56"
     Next i
     Sheets("Summary").Range("J4").Consolidate Sources:=(ConsolidationArray), Function:=xlAverage
End Sub