Insert formula in range of cells through VBA Code

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,531
Office Version
  1. 2016
Platform
  1. Windows
Hello Firends,

I am trying to insert the formula in the range of cells through a VBA code as below but it says that "Unable to set the formula array property of the range class" for the second formula. The first one works just fine. Can anyone please check & let me know what have I done wrong

This one works
VBA Code:
ws.Range("AC2").FormulaArray = "=IFERROR(INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),"""")"
ws.Range("AC2:AC2000").FillDown

But this does not
VBA Code:
ws.Range("AC2").FormulaArray = "=IFERROR(IF(AB2=""Not Applicable"",""Not Applicable"",IF(INDEX(fri_dpi_labtest_quantity,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 1""=fri_dpi_labtest_category),0))<L2*98%,INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),""Not Applicable"")),"""")"
ws.Range("AC2:AC2000").FillDown

Regards,

Humayun
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
See the blue below:
I assume you only need the 2nd one.
Since you have MS365 Formula2 will enter an array formula.

Rich (BB code):
ws.Range("AC2").Formula2 = "=IFERROR(INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),"""")"
ws.Range("AC2:AC2000").FillDown


ws.Range("AC2").Formula2  = "=IFERROR(IF(AB2=""Not Applicable"",""Not Applicable"",IF(INDEX(fri_dpi_labtest_quantity,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 1""=fri_dpi_labtest_category),0))<L2*98%,INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),""Not Applicable"")),"""")"
ws.Range("AC2:AC2000").FillDown
 
Upvote 0
Your array formula is over 255 characters, which is why it doesn't work. Since you appear to be using 2016, Formula2 won't help you.

You might be able to get away with using:

Code:
ws.Range("AC2").FormulaArray = "=IFERROR(IF(AB2=""NA"",""NA"",IF(INDEX(fri_dpi_labtest_quantity,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 1""=fri_dpi_labtest_category),0))<L2*98%,INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),""NA"")),"""")"
With ws.Range("AC2:AC2000")
   .FillDown
   .Replace "NA", "Not Applicable", xlpart
end with
 
Upvote 0
Solution
Your array formula is over 255 characters, which is why it doesn't work. Since you appear to be using 2016, Formula2 won't help you.

You might be able to get away with using:

Code:
ws.Range("AC2").FormulaArray = "=IFERROR(IF(AB2=""NA"",""NA"",IF(INDEX(fri_dpi_labtest_quantity,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 1""=fri_dpi_labtest_category),0))<L2*98%,INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),""NA"")),"""")"
With ws.Range("AC2:AC2000")
   .FillDown
   .Replace "NA", "Not Applicable", xlpart
end with
Hi Rory,

Thanks for the reply -

After replacing Not Applicable to NA, the formula still contains over 260 characters - therefore, I had to remove the iferror to get it down to under 255 characters
Now it is working just fine

Now that I know what was causing it not to work - I can & will play with a bit if I have to have the iferror in any case - like I can rename the named ranges with short names etc or using some helper columns

Regards,

Humayun
 
Upvote 0
See the blue below:
I assume you only need the 2nd one.
Since you have MS365 Formula2 will enter an array formula.

Rich (BB code):
ws.Range("AC2").Formula2 = "=IFERROR(INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),"""")"
ws.Range("AC2:AC2000").FillDown


ws.Range("AC2").Formula2  = "=IFERROR(IF(AB2=""Not Applicable"",""Not Applicable"",IF(INDEX(fri_dpi_labtest_quantity,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 1""=fri_dpi_labtest_category),0))<L2*98%,INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),""Not Applicable"")),"""")"
ws.Range("AC2:AC2000").FillDown
Hi Alex,
I don't have 365 version as @RoryA pointed out
but thanks anyways for taking your time
Regards,
humayun
 
Upvote 0
Book1
A
1 =IFERROR(IF(AB2=""NA"",""NA"",IF(INDEX(fri_dpi_labtest_quantity,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 1""=fri_dpi_labtest_category),0))<L2*98%,INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),""NA"")),"""")
2261
Sheet1
Cell Formulas
RangeFormula
A2A2=LEN(A1)
 
Upvote 0

Forum statistics

Threads
1,224,749
Messages
6,180,725
Members
452,995
Latest member
isldboy

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