Unholy Mess of a Formula, Please Help!

JeffK627

Active Member
Joined
Jun 22, 2005
Messages
313
I have the following nightmare (line breaks added for legibility):

Code:
Sheet2.Range(.Cells(5, i + 11), .Cells(lastRow, i + 11)).Formula = _
"=CHOOSE(MATCH(RC4,""No Category Defined"",
""No MCO"",
""MCO – PB thru FFS"",
""MCO – PB thru MCO – Sub – Sep"",
""MCO – PB thru MCO – Sub – Comb"",
""MCO – PB thru MCO – Not Sub"",
""MCO – PB through FFS & MCO – Sub – Sep"",
""MCO – PB through FFS & MCO – Sub – Comb"",
""MCO – PB through FFS & MCO – Not Sub "",0), 
0, RC[-4], RC[-4], RC[-4], 
RC[-5] * AVERAGE(IF(C4=""MCO – PB thru MCO – Sub – Sep"", C[-2]:C[-2])), 
(RC[-5] * (AVERAGE(IF(C4=""MCO – PB thru MCO – Sub – Sep"", C[-2]:C[-2])))) / (1-(AVERAGE(IF(C4=""MCO – PB thru MCO – Sub – Sep"", C[-2]:C[-2])))), 
RC[-4], 
RC[-5] * (AVERAGE(IF(C4=""MCO – PB through FFS & MCO – Sub – Sep"", C[-2]:C[-2]))), 
(RC[-5] * (AVERAGE(IF(C4=""MCO – PB through FFS & MCO – Sub – Sep"", C[-2]:C[-2])))) / (1-(AVERAGE(IF(C4=""MCO – PB through FFS & MCO – Sub – Sep"", C[-2]:C[-2])))))"

The idea is to enter a different formula into a specified column of a spreadsheet depending on the value in the corresponding row in column D.

The above would work fine, except that some of the formulas are simple formulas and some are array formulas. The array formulas cause an error when the VBA code gets to this line. I tried using "Sheet2.Range(.Cells(5, i + 11), .Cells(lastRow, i + 11)).FormulaArray =" but that also threw an error. I also tried enclosing the array formulas in curly brackets but that didn't work either. Can anyone help?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You can't define a formula for the cell within a formula. To do that you need to examine each possibililty and execute a different .Formula = or .Value statement as appropriate.

I would also use VBA find instead of MATCH and then do a Select Case based on the result.
 
Upvote 0
I'm trying to avoid looping through all the cells in VBA because there are literally tens of thousands of rows in this thing and I don't want the performance hit.
 
Upvote 0
AutoFilter on column D for the different values and then place the specific formula for that filtered value in the visible cells of the other columns.

You would loop the autofilter values in column D but place the formulas en masse in the visible results.
 
Upvote 0
I suspect - with tens of thousands of rows - solutions using formulas, especially array formulas, will be slow. If you find it is slow, I suggest you consider non-formula approaches using SQL. So a query instead of a large worksheet of formulas: maybe a query table or VBA/ADO or even pivot table.
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,569
Members
452,926
Latest member
rows and columns

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