Array Formula in VBA with SUM and IF not working

jacksongf

New Member
Joined
Mar 23, 2017
Messages
21
I'm trying to apply an array formula to a range of cells, however it gives me the classic "Unable to set the FormulaArray property of the Range class" error.

Here is the equation:

Code:
ActiveSheet.Cells(i, 30).FormulaArray = "=Sum(If('" & Path & "\[" & FileName & "]" & SheetName & "'!" & "B5" & ":" & "B371" & "=" & """Mon""" & "," & "'" & Path & "\[" & FileName & "]" & SheetName & "'!" & "T5" & ":" & "T371" & "," & "0" & "))"

The weird thing is, if I take out the = sign before the Sum, then it will paste this formula as text in the cells, and then if I insert an equal sign manually and press ctrl + alt + enter, it works perfectly.

So why isn't it working in VBA then?

Any help is greatly appreciated!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Last edited:
Upvote 0
Thanks for the response Marcelo,

The problem is that simply filling down or looping won't work, because the formula relies on what's in the row with it (the variables Path, Filename, Sheetname etc). It need's to lookup to a certain path, file name, and sheet name withing that file. Unless there's a way to make this work in a formula that can be pulled down... I'll play around with it a bit.

I'll also read the article you posted. Thanks!

Jackson
 
Upvote 0
It's hard to help...
Some questions:
Why are you using an array formula like SUM(IF(...? Couldn't you use SUMIFS(...?
Also i don't understand
..& SheetName & "'!" & "B5" & ":" & "B371" & "=" & """Mon""" & ","...
Why not?
..& SheetName & "'!B5:B371=""Mon"",...

M.
 
Upvote 0
Complementing my previous post
I know SUMIFS doesn't work with closed workbooks, but maybe you can build an equivalent formula with SUMPRODUCT that works perfectly with closed workbooks, avoiding an Ctrl+Shift+Enter formula

Just thoughts...as i said is hard to help..

M.
 
Last edited:
Upvote 0
I'm using SUM(IF( because of the closed workbook issue, and I believe the formula with B5:B371 as not text would work as well, I just decided to keep everything as text and input it like that, but what you said would work as well I think.

I am not very familiar with the SUMPRODUCT formula, how would I go about using that to achieve my goal and replace the need for an array formula?

No worries, I realize it is hard to help, but I greatly appreciate your responses.
 
Upvote 0
Example of SUMPRODUCT equivalent to SUMIFS

Suppose you have
=SUMIFS(C2:C10,A2:A10,criteria1,B2:B10,criteria2)

an equivalent formula would be
=SUMPRODUCT(C2:C10,--(A2:A10=criteria1),--(B2:B10=criteria2))

For a very good explanation about SUMPRODUCT with multiple criteria take a look at
http://xldynamic.com/source/xld.SUMPRODUCT.html

M.
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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