How to specify used ranges for data consolidation with VBA

techbutterfly

New Member
Joined
Mar 28, 2019
Messages
2
This is my first time posting here, though I've gotten plenty of answers from these forums in the past.

I am using the data consolidation feature in Excel 2013. Because the two sheets I'm consolidating might have different sized ranges each time, I want to capture the used range for each sheet and use them in the consolidate command. I'm getting a type mismatch, but I'm not sure how to resolve it. Here is my code.

Code:
Sub tec_agr_consolidate()
'
' tec_agr_consolidate Macro
'
Dim TEC As Range
Dim IVUE As Range

Worksheets(1).Activate
Set TEC = Worksheets(1).UsedRange

Worksheets(2).Activate
Set IVUE = Worksheets(2).UsedRange

    Sheets.Add After:=ActiveSheet
    Range("A1").Select
    Selection.Consolidate Sources:=Array( _
        TEC, IVUE), _
        Function:=xlSum, TopRow:=True, LeftColumn:=True, CreateLinks:=False
        
End Sub

Any help is appreciated!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi
Welcome to the board

When in doubt always check the help:

https://docs.microsoft.com/en-us/office/vba/api/excel.range.consolidate

In this case notice:

Code:
The sources of the consolidation as an array of text reference strings  in R1C1-style notation.
 The references must include the full path of  sheets to be consolidated.

Instead of the array of ranges, that you used, use an array with the addressed in in R1C1-style notation including the sheet name, for ex.:

Code:
Sub tec_agr_consolidate()
'
' tec_agr_consolidate Macro
'
Dim sTEC As String
Dim sIVUE As String

sTEC = Worksheets(1).UsedRange.Address(ReferenceStyle:=xlR1C1, External:=True)

sIVUE = Worksheets(2).UsedRange.Address(ReferenceStyle:=xlR1C1, External:=True)

    Worksheets.Add After:=Worksheets(2)
    Range("A1").Consolidate Sources:=Array( _
        sTEC, sIVUE), _
        Function:=xlSum, TopRow:=True, LeftColumn:=True, CreateLinks:=False
        
End Sub
 
Upvote 0
Thanks so much for the help - worked great! I had looked at that help page, but did not read the description for source carefully enough. Next time I'll look closer.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top