Combining Multiple Array Formulas in a single cell using VBA

Moosles

New Member
Joined
Apr 1, 2021
Messages
21
Office Version
  1. 2019
Platform
  1. Windows
I have 5 tabs of data which have varying numbers of columns and varying numbers of rows. The data that gets input into these tabs will be regularly changed, and the number of rows and columns with each input will be an unknown variable.

The tabs of data look like this (highly simplified for this example):

Tab 1June 2023July 2023August 2023September 2023
Department 13810
Department 27299
Department 39334
Department 43857
Department 55032
Department 61516
Department 75827
Department 83364
Department 99543

Tab 2August 2023September 2023October 2023November 2023December 2023
Department 681022
Department 298245
Department 944332
Department 157743

And I'm looking to combine the various tabs into one table as part of a wider macro:

TOTALJune 2023July 2023August 2023September 2023October 2023November 2023December 2023
Department 13867743
Department 2721711245
Department 39334000
Department 43857000
Department 55032000
Department 61597022
Department 75827000
Department 83364000
Department 99577332

Currently I have a macro that creates the "TOTAL" table that determines the earliest overall date from the 5 tabs as the starting column, and the latest overall date from each of the 5 tabs as the end column, and I want to apply a formula to sum up all the 5 tabs into one and the simplest way I can think of is to apply an array formula that pulls the relevant data from all 5 tabs. For example if the Departments were all in A5:A50 of each of the tabs, and the dates were all in E2:J2 of each of the tabs, and the data was in E5:J50 for all tabs then the formula I would want to add would be:

Excel Formula:
=SUM(SUM(IF('Tab 1'!A5:A50=B6,IF('Tab 1'!E2:J2=D3,'Tab 1'!E5:J50))),
SUM(IF('Tab 2'!A5:A50=B6,IF('Tab 2'!E2:J2=D3,'Tab 2'!E5:J50))),
SUM(IF('Tab 3'!A5:A50=B6,IF('Tab 3'!E2:J2=D3,'Tab 3'!E5:J50))),
SUM(IF('Tab 4'!A5:A50=B6,IF('Tab 4'!E2:J2=D3,'Tab 4'!E5:J50))),
SUM(IF('Tab 5'!A5:A50=B6,IF('Tab 5'!E2:J2=D3,'Tab 5'!E5:J50))))

But when I try to add this via VBA it breaks and I get a Run Time Error '1004': Unable to set the FormulaArray property of the Range class. The VBA code I tested is this:

VBA Code:
Range("D6").FormulaArray = "=SUM(SUM(IF('Tab 1'!R5C1:R50C1=RC1,IF('Tab 1'!R2C5:R2C10=R3C,'Tab 1'!R5C5:R50C10)))," _
& "SUM(IF('Tab 2'!R5C1:R50C1=RC2,IF('Tab 2'!R2C5:R2C10=R3C,'Tab 2'!R5C5:R50C10)))," _
& "SUM(IF('Tab 3'!R5C1:R50C1=RC2,IF('Tab 3'!R2C5:R2C10=R3C,'Tab 3'!R5C5:R50C10)))," _
& "SUM(IF('Tab 4'!R5C1:R50C1=RC2,IF('Tab 4'!R2C5:R2C10=R3C,'Tab 4'!R5C5:R50C10)))," _
& "SUM(IF('Tab 5'!R5C1:R50C1=RC2,IF('Tab 5'!R2C5:R2C10=R3C,'Tab 5'!R5C5:R50C10))))"

When I test the individual formulas it works absolutely fine:

VBA Code:
Range("D6").FormulaArray = "=SUM(IF('Tab 1'!R5C1:R50C1=RC1,IF('Tab 1'!R2C5:R2C10=R3C,'Tab 1'!R5C5:R50C10)))"
Range("D6").FormulaArray = "=SUM(IF('Tab 2'!R5C1:R50C1=RC2,IF('Tab 2'!R2C5:R2C10=R3C,'Tab 2'!R5C5:R50C10)))"
Range("D6").FormulaArray = "=SUM(IF('Tab 3'!R5C1:R50C1=RC2,IF('Tab 3'!R2C5:R2C10=R3C,'Tab 3'!R5C5:R50C10)))"
Range("D6").FormulaArray = "=SUM(IF('Tab 4'!R5C1:R50C1=RC2,IF('Tab 4'!R2C5:R2C10=R3C,'Tab 4'!R5C5:R50C10)))"
Range("D6").FormulaArray = "=SUM(IF('Tab 5'!R5C1:R50C1=RC2,IF('Tab 5'!R2C5:R2C10=R3C,'Tab 5'!R5C5:R50C10)))"

I stepped through each one and it does exactly what I want it to do to each tab, but combining the formulas into one seems to break it. What do I need to do to get around this?

Eventually I will apply terms that determine the start and end rows and columns of each sheet:

VBA Code:
Tab1EndRow = Sheets("Tab 1").Range("B" & Rows.Count).End(xlUp).Row
Tab2EndRow = Sheets("Tab 2").Range("B" & Rows.Count).End(xlUp).Row
Tab3EndRow = Sheets("Tab 3").Range("B" & Rows.Count).End(xlUp).Row
Tab4EndRow = Sheets("Tab 4").Range("B" & Rows.Count).End(xlUp).Row
Tab5EndRow = Sheets("Japan").Range("B" & Rows.Count).End(xlUp).Row

Tab1EndColumn = Sheets("Tab 1").Range("XFD5").End(xlToLeft).Column
Tab2EndColumn = Sheets("Tab 2").Range("XFD5").End(xlToLeft).Column
Tab3EndColumn = Sheets("Tab 3").Range("XFD5").End(xlToLeft).Column
Tab4EndColumn = Sheets("Tab 4").Range("XFD5").End(xlToLeft).Column
Tab5EndColumn = Sheets("Tab 5").Range("XFD5").End(xlToLeft).Column

But first I need to understand the issue in the simplified version above, any ideas?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
An example using Power Query:

Power Query:
let
    tablenames = {"Table4", "Table5"},
    lstTables = List.Accumulate(tablenames, {}, (s,c)=> s &{Excel.CurrentWorkbook(){[Name=c]}[Content]}),
    tbl3 = Table.Combine(lstTables),
    tcn = Table.ColumnNames(tbl3),
    tbl4 = Table.ReplaceValue(tbl3, null, 0, Replacer.ReplaceValue, tcn),
    lst = List.Accumulate(List.Skip(tcn,1), {}, (s,c)=> s & {{c, each List.Sum(Record.Field(_,c)), type number}}),
    Result = Table.Group(tbl4, {"Dept"}, lst)
in
    Result

Book1
ABCDEFGHIJKLMNOPQR
1DeptABCDeptBCDEDeptABCDE
2Dept111814Dept32017219Dept11181400
3Dept241213Dept468111Dept24121300
4Dept3176Dept51516314Dept312723219
5Dept4592Dept691875Dept451510111
6Dept531510Dept74121013Dept533026314
7Dept6091875
8Dept704121013
9
Sheet2
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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