I am currently trying to make a system where a user can select some checkboxes that refer to tables and get a consolidated table of the ones that were selected. So far my system generates the checkboxes, checks which ones are ticked, passes that list to another function which is supposed to read the ranges for the tables that have been selected and pass that range to a consolidate function to create the final table.
I'm having trouble getting the consolidation function to work. From what I gather the .Consolidation function requires an array of ranges in string form to work, but no matter how I try to pass the ranges I can't seem to get the function to work for me
Below is the code that generates the array, while also creating a combined table on another worksheet so I could make sure that it is actually running through. The combined table is made without any trouble.
Function rangesfromtables(workinglist() As Variant) As Variant
Dim tbl As ListObject
Dim sht As Worksheet
Dim workingrange As Range
Dim workingarray() As Variant
Dim item As Variant
Dim loopcount As Integer
Dim destinationsheet As Worksheet
Dim endrow As Long
Dim numrows As Long
Set destinationsheet = ThisWorkbook.Worksheets("WorkingSheet")
destinationsheet.Cells.Clear
loopcount = 0
endrow = 1
For Each item In workinglist
'Loop through each sheet and table in the workbook
For Each sht In ThisWorkbook.Worksheets
For Each tbl In sht.ListObjects
If StrComp(item, tbl.name, vbTextCompare) = 0 Then
If loopcount = 0 Then
Set workingrange = tbl.Range
ReDim workingarray(0)
workingarray(UBound(workingarray)) = sht.name & tbl.Range.Address(ReferenceStyle:=xlR1C1)
loopcount = loopcount + 1
Else
Set workingrange = tbl.DataBodyRange
ReDim Preserve workingarray(UBound(workingarray) + 1)
workingarray(UBound(workingarray)) = sht.name & tbl.Range.Address(ReferenceStyle:=xlR1C1)
End If
numrows = workingrange.Rows.Count 'Below code copies table data to separate worksheet for checking
workingrange.Copy
destinationsheet.Range("A" & endrow).PasteSpecial Paste:=xlPasteValues
endrow = endrow + numrows
End If
Next tbl
Next sht
Next item
rangesfromtables = workingarray
End Function
This is the function that is supposed to consolidate the tables
Sub consolidatetable(workingrange() As Variant)
Dim destinationsheet As Worksheet
Set destinationsheet = ThisWorkbook.Worksheets("Main Sheet")
destinationsheet.Cells.Clear
destinationsheet.Range("A6").Consolidate _
Sources:=workingrange, _
Function:=x1Sum, _
TopRow:=True, _
LeftColumn:=True, _
CreateLinks:=False
End Sub
Whenever I run the code I get the error 1004 Consolidate method of Range class failed
I have a feeling that my problem is putting the ranges of the tables into the array incorrectly, but I have tried many different ways and I can't seem to do it. I've tried having a string array instead of variant, tried passing the ranges without modifying them, at the moment I'm attempting to turn the range into a string, but I don't know if I'm doing it correctly.
Any help would be appreciated.
A small update, even when I put a range in manually, I still get the error, but I feel like I'm using the function correctly according to the documentation
Sub consolidatetable(workingrange() As Variant)
Dim destinationsheet As Worksheet
Set destinationsheet = ThisWorkbook.Worksheets("Main Sheet")
destinationsheet.Cells.Clear
destinationsheet.Range("A6").Consolidate _
Sources:="WorkingSheet!A1:J23", _
Function:=x1Sum, TopRow:=True, LeftColumn:=True, CreateLinks:=False
End Sub



!aftersht.Nameand before the table R1C1-Address. Not sure that's going to fix your problem, but that would be a step in the right direction. Also, you might need to add apostrophes (') surrounding the sheet name if they can have spaces in them.Sources:=Array("WorkingSheet!R1C1:R23C10","OtherSheet!R1C1:R23C10")destinationsheet.Range("A6").Consolidate _ Sources:=Array("ESK12!R5C1:R15C9", "ESK12!R19C1:R25C9"), _ Function:=x1Sum, TopRow:=True, LeftColumn:=True, CreateLinks:=FalseThis is doing my head in. Maybe I should give up on consolidate and use a pivot table on the consolidated table I made for testing. I was just hoping to not need to do it that way.rangesfromtablesfunction? You can doDebug.Print Join(workingarray, ", ")at the end of your function. Your presented ranges are on the same worksheet and of different sizes: that's not whatConsolidatehandles. Look at DecimalTurn's example: different worksheets but the same range sizes.