XLSum in Consolidation Array Workbook

Trevor G

Registered User.
Local time
Today, 23:29
Joined
Oct 1, 2009
Messages
2,361
I have created a consolidation option to open multiple books and I have recorded this option within Excel 2007. It works great but I would like to select the workbooks when I need them, rather than the ones indicated. So I believe I should refer them in an array but not sure how to amend the code. Any advice would be welcome.

The folder path indicated is an example but will change, but the workbooks will come from the same folder location.

Sub mcrConsolidate()
'
' mcrConsolidate Macro
'
'
Range("A2").Select
Selection.Consolidate Sources:=Array( _
"'C:\Access VBA Practice\Consolidate1.xls'!R2C1:R5C3", _
"'C:\Access VBA Practice\Consolidate2.xls'!R2C1:R5C3", _
"'C:\Access VBA Practice\Consolidate3.xls'!R2C1:R5C3"), Function:=xlSum, _
TopRow:=True, LeftColumn:=True, CreateLinks:=False
End Sub
 
I have found Chip Pearson's function to consolidate, but it fails on the array side.

The Function is shown here

Sub consolidateall2()
Dim DestCell As Range
Dim DataColumn As Variant
Dim NumberOfColumns As Variant
Dim WB As Workbook
Dim DestWB As Workbook
Dim WS As Worksheet
Dim FileNames As Variant
Dim N As Long
Dim R As Range
Dim StartRow As Long
Dim LastRow As Long
Dim RowNdx As Long
Set DestWB = ActiveWorkbook
' DestCell is the first cell where the consolidated
' data will be written.
Set DestCell = DestWB.Worksheets("Sheet1").Range("C12:F32")
' DataColumn is the column on the worksheets to be
' consolidated where the actual data is. Data will
' be copied from this column.
DataColumn = "c"
' NumberOfColumns is the number of columns on each
' worksheet to be consolidated from which data will
' be copied. E.g., if your data is in range A1:J100,
' NumberOfColumns would be 10.
NumberOfColumns = 4
' StartRow is the row on the worksheets to be consolidated
' where the data starts. If your worksheet have heading/summary
' rows at the top, set this value to the row number where
' the actual data starts.
StartRow = 12
' Get the workbooks to consolidate
FileNames = Application.GetOpenFilename( _
filefilter:="Excel Files (*.xls*),*.xls*", _
Title:="Select the workbooks to merge.", MultiSelect:=True)
If IsArray(FileNames) = False Then
If FileNames = False Then
' User cancelled open dialog. get out.
Exit Sub
End If
End If
' Loop through all the selected files.
For N = LBound(FileNames) To UBound(FileNames)
' Open the workbook
Set WB = Workbooks.Open(Filename:=FileNames(N), ReadOnly:=True)
' Loop through all the worksheets in the workbook
For Each WS In WB.Worksheets
Range("C12:F32").Consolidate Array("Sheet1").Range("c12:F32"), Function:=xlSum
Next WS
' close the workbook.
WB.Close savechanges:=False
Next N
End Sub

But it falls over:

Range("C2:F32").Consolidate Sources:=Array("Sheet1!R12C3:R32C6"), Function:=xlSum

I have added Option Explicit to see if it helps but no it doesn't. The run time error is 1004 Source reference overlaps destination area.
 

Users who are viewing this thread

Back
Top Bottom