[英]Excel get cell value from multiple workbooks
Good morning all, 大家早上好,
I'm looking for possible alternatives to my current system of importing data from multiple "unit" workbooks into a "master" workbook. 我正在寻找将当前数据从多个“单位”工作簿导入“主”工作簿的系统的替代方案。 Each of my "unit" workbooks is built from a fixed template, so their sheet names and cell references are always the same. 我的每个“单元”工作簿都是从固定模板构建的,因此它们的工作表名称和单元格引用始终相同。 (Sheet names are: Jan, Feb, Mar, Apr, ... Dec)(File names are: YYYY_Location_Description) (工作表名称是:一月,二月,三月,四月,...十二月)(文件名是:YYYY_Location_Description)
Ideally I would like to be able to simply do a sumproduct through all open workbook files. 理想情况下,我希望能够通过所有打开的工作簿文件简单地进行求和。 I know I can do it through sheets using Jan:Dec!A2:D5. 我知道我可以使用Jan:Dec!A2:D5来完成工作。 But is there a way to adapt this to run through all open workbook files? 但是,是否有办法使它适应所有打开的工作簿文件?
Currently I have a macro that systematically runs through all files listed by DIR and copies the data out of the "unit" sheets one entry at a time and pastes it in a summary sheet. 目前,我有一个宏,可以系统地运行DIR列出的所有文件,并将数据一次一次从“单元”表中复制出来,然后粘贴到摘要表中。
Each monthly "unit" sheet consists of a row for each day of the month and columns as shown: 每个月的“单位”表都包含该月的每一天的一行和各列,如下所示:
UnitA
Date HrsA HrsB ValueA ValueB ValueC ...
1 24 0 0.01 0.02 0.01
2 0 24 0.01 0.015 0.012
...
The macro copied data is then placed in the format: 然后将宏复制的数据放置为以下格式:
Master
Date Location Value HrsA HrsB Entry
1 UnitA ValueA 24 0 0.01
1 UnitA ValueB 24 0 0.02
1 UnitA ValueC 24 0 0.01
1 UnitA ValueA 0 24 0.01
1 UnitA ValueB 0 24 0.015
1 UnitA ValueC 0 24 0.012
Which is a lot of data in the "master" summary sheet. “主”摘要表中有很多数据。 What I'd rather be able to do is: 我希望能够做的是:
=sum('[Workbook1.xls:WorkbookN.xls]Jan:Dec'!$A$2:$D$5)
Is it possible to do anything like this? 有可能做这样的事情吗?
Thanks, 谢谢,
Try something like this, just loop over each of the open workbooks and evaluate the Sum
of the specified range, and then write that value to ActiveCell (or wherever it needs to be placed) 尝试这样的操作,只需遍历每个打开的工作簿并评估指定范围的Sum
,然后将该值写入ActiveCell(或需要放置在任何位置)
Sub foo()
Dim wb as Workbook
Dim ret
For Each wb in Application.Workbooks
'[Workbook1.xls:WorkbookN.xls]Jan:Dec'!$A$2:$D$5)
ret = ret + Application.WorksheetFunction.Sum( _
"'[" & wb.Name & "]Jan:Dec'!$A$2:$D$5")
Next
ActiveCell.Value = ret
End Function
Or try this to build the formula string: 或尝试使用以下方法来构建公式字符串:
Sub foo(Optional sheetRange As String = "Jan:Dec", Optional cellsRange As String = "A2:D5")
Dim wb As Workbook
Dim ret
For Each wb In Application.Workbooks
ret = ret & "SUM('[" & wb.Name & "]" & sheetRange & "'!" & cellsRange & ")+"
Next
ret = Left(ret, Len(ret) - 1)
ret = "=" & ret
ActiveCell.Formula = ret
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.