0
votes

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)

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. 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.

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,

1

1 Answers

0
votes

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)

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