Combining working .FormulaArray to working formula

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I have two formulas that I am trying to put together. They work on their own, but not when put together. What am I missing?

Excel Formula:
Range("B4").FormulaArray = "=(SUM(--(MMULT(--IF((" & subarea & "=RIGHT(A4,3))*(" & dept & "=""SA""),(" & qtr & "=""N""),0),TRANSPOSE(COLUMN(" & qtr & ")^0))>0)))"
and
Excel Formula:
Range("B7") = "=ROUNDDOWN(1-(B4/(COUNTIFS(" & dept & ",""SA""," & subarea & ",RIGHT(A4,3)))),2)"
I would have thought this -
Excel Formula:
Range("B7").FormulaArray = "=ROUNDDOWN(1-((SUM(--(MMULT(--IF((" & subarea & "=RIGHT(A4,3))*(" & dept & "=""SA""),(" & qtr & "=""N""),0),TRANSPOSE(COLUMN(" & qtr & ")^0))>0)))/(COUNTIFS(" & dept & ",""SA""," & subarea & ",RIGHT(A4,3)))),2)"
But I get "Unable to set FormulaArray property of the Range class"

Thank you
 
It looks like the limit is actually 180 characters, not 255 as shown on the page that @Marcelo Branco has linked to.

The only workaround that comes to mind it to use named ranges instead of sheet name and cell range in the formula.
VBA Code:
Sub test()
With ActiveWorkbook.Names
    .Add Name:="SubArea", RefersTo:="=Employees!C4:C5000"
    .Add Name:="dept", RefersTo:="=Employees!H4:H5000"
    .Add Name:="qtr", RefersTo:="=Employees!K4:N5000"
End With
Range("B7").FormulaArray = ("=ROUNDDOWN(1-((SUM(--(MMULT(--IF((subarea=RIGHT(A4,3))*(dept=""SA""),(qtr=""N""),0),TRANSPOSE(COLUMN(qtr)^0))>0)))/(COUNTIFS(dept,""SA"",subarea,RIGHT(A4,3)))),2)")
End Sub
 
Upvote 0
Solution

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
It looks like the limit is actually 180 characters, not 255 as shown on the page that @Marcelo Branco has linked to.

The only workaround that comes to mind it to use named ranges instead of sheet name and cell range in the formula.
VBA Code:
Sub test()
With ActiveWorkbook.Names
    .Add Name:="SubArea", RefersTo:="=Employees!C4:C5000"
    .Add Name:="dept", RefersTo:="=Employees!H4:H5000"
    .Add Name:="qtr", RefersTo:="=Employees!K4:N5000"
End With
Range("B7").FormulaArray = ("=ROUNDDOWN(1-((SUM(--(MMULT(--IF((subarea=RIGHT(A4,3))*(dept=""SA""),(qtr=""N""),0),TRANSPOSE(COLUMN(qtr)^0))>0)))/(COUNTIFS(dept,""SA"",subarea,RIGHT(A4,3)))),2)")
End Sub
Workbook names is new to me, seems similar to String which is what I was using. I made the changes to my formula, and Bingo!

Thank you very much.

VBA Code:
    With ActiveWorkbook.Names
    .Add Name:="SubArea", RefersTo:="=Employees!$C$4:$C$5000"
    .Add Name:="dept", RefersTo:="=Employees!$H$4:$H$5000"
    .Add Name:="qtr", RefersTo:="=Employees!$K$4:$N$5000"
    End With
    
    Range("B4").FormulaArray = "=ROUNDDOWN(1-((SUM(--(MMULT(--IF(dept=""SA"",(qtr=""N""),0),TRANSPOSE(COLUMN(qtr)^0))>0)))/(COUNTIFS(dept,""SA""))),2)"
 
Upvote 0
It looks like the limit is actually 180 characters, not 255 as shown on the page that @Marcelo Branco has linked to.

The only workaround that comes to mind it to use named ranges instead of sheet name and cell range in the formula.
VBA Code:
Sub test()
With ActiveWorkbook.Names
    .Add Name:="SubArea", RefersTo:="=Employees!C4:C5000"
    .Add Name:="dept", RefersTo:="=Employees!H4:H5000"
    .Add Name:="qtr", RefersTo:="=Employees!K4:N5000"
End With
Range("B7").FormulaArray = ("=ROUNDDOWN(1-((SUM(--(MMULT(--IF((subarea=RIGHT(A4,3))*(dept=""SA""),(qtr=""N""),0),TRANSPOSE(COLUMN(qtr)^0))>0)))/(COUNTIFS(dept,""SA"",subarea,RIGHT(A4,3)))),2)")
End Sub
BTW where did you come up with 180 characters. I read both articles and they both mention 255. Is it my version of Office? Thank you
 
Upvote 0
BTW where did you come up with 180 characters.
Trial and error. I had tested with a shorter version that worked fine (single cell references for each range). Given that the only difference between it and yours was the length of the formula I just made changes to the ranges and sheetnames to reduce the character count until it worked.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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