asked on # Big formula error when using VBA

Hi, I have the following formula that I am trying to enter using VBA and each time, I am getting 'Application Defined' error.

I am thinking, it has to do with the size of formula (no !@#$ lol) Is there any go around for this? I tried assigning whole formula to a string, then to the activecell formula, and still same error! The formula is perfectly working formula.

Any help is appreciated!

Thanks!

I am thinking, it has to do with the size of formula (no !@#$ lol) Is there any go around for this? I tried assigning whole formula to a string, then to the activecell formula, and still same error! The formula is perfectly working formula.

Any help is appreciated!

ActiveCell.FormulaR1C1 = _

"=IF(AND(LEN(RC[4])>2,IFERROR(MAX(VL OOKUP(RC1, Packing_Sl ip,11,FALS E),VLOOKUP (RC1,Packi ng_Slip,15 ,FALSE),VL OOKUP(RC1, Packing_Sl ip,19,FALS E)),0)>0), IF(AND(COU NTIFS('PO & SKU'!C4,""=MULTI*"")>0,ISN A(VLOOKUP( RC1,Packin g_Slip,8,F ALSE))=FAL SE),1,"""" ),IF(AND(R C10>0,RC[4 ]<>""MULTI SKU""),ROU NDUP((SUMI FS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=S"")+SUMIFS('P O & SKU" & _

"O & SKU'!C2,RC1,'PO & SKU'!C21,""=D"")+SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=P"")-SUMIFS('P O & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=O"")+RC10)/(SU MIF('PO & SKU'!C2,RC1,'PO & SKU'!C7)),2),IF(RC[4]=""MU LTISKU"",I F(COUNTIF( 'PO & SKU'!C2,'CONTAINER DATA'!R[4410]C1)=COUNTIFS( 'PO & SKU'!C11,""<>"",'PO & SKU'!C2,'CONTAINER DATA'!R[4410]C1),1,(CO" & _

"O & SKU'!C11,""<>"",'PO & SKU'!C2,'CONTAINER DATA'!R[4410]C1)/COUNTIF('PO & SKU'!C2,'CONTAINER DATA'!R[4410]C1))),""""))) "

Thanks!

Microsoft ExcelMicrosoft Office

View this solution by signing up for a free trial.

Members can start a 7-Day free trial and enjoy unlimited access to the platform.

Thank you. That is indeed the last option but I do want to squeeze it in if possible through any other methods?

View this solution by signing up for a free trial.

Members can start a 7-Day free trial and enjoy unlimited access to the platform.

You could write some functions of your own that will perform some the calculations and then call these functions. This would achieve the same thing except the calculations would only be performed as required. Using Application.WorksheetFunction you can still call worksheet functions so that you do not need to reinvent the wheel

Or as suggested above you could develop a function that performs the entire calcaluation and then just call this function.

View this solution by signing up for a free trial.

Members can start a 7-Day free trial and enjoy unlimited access to the platform.

View this solution by signing up for a free trial.

Members can start a 7-Day free trial and enjoy unlimited access to the platform.

Thanks all!

So the 'IfError' was masking my actual errors!

So the 'IfError' was masking my actual errors!

I think Rorya's point is that the function within the formula doesn't look right.

Syntax for SUMIFS = SUMIFS(SumRange, CriteriaRange1, Criteria1, etc)

The formula above only has a single cell for the SumRange.

Thanks

Rob H

Syntax for SUMIFS = SUMIFS(SumRange, CriteriaRange1, Criteria1, etc)

The formula above only has a single cell for the SumRange.

Thanks

Rob H

It was the sheet names that looked wrong. (remember it's R1C1 referencing so C2 = column B)

Sorry guys, I came back to work and the problem still exits. Partly because I pasted wrong 'VBA version of formula' in my question above

Here is the actual Excel Formula and below this is the VBA version. (Rob H, the sumifs is correct as its a '$C:$C' which would translate into C3)

Excel

VBA

Error:

"Application Defined or Object Defined error"

Thanks again!

Here is the actual Excel Formula and below this is the VBA version. (Rob H, the sumifs is correct as its a '$C:$C' which would translate into C3)

Excel

=IF(AND(LEN(O2)>2,IFERROR(MAX(VLOOKU P($A2,Pack ing_Slip,1 1,FALSE),V LOOKUP($A2 ,Packing_S lip,15,FAL SE),VLOOKU P($A2,Pack ing_Slip,1 9,FALSE)), 0)>0),IF(A ND(COUNTIF S('PO & SKU'!$D:$D,"=MULTI*")>0,IS NA(VLOOKUP ($A2,Packi ng_Slip,8, FALSE))=FA LSE),1,"") ,IF(AND($J 2>0,O2<>"M ULTISKU"), ROUNDUP((S UMIFS('PO & SKU'!$V:$V,'PO & SKU'!$B:$B,$A2,'PO & SKU'!$U:$U,"=S")+SUMIFS('P O & SKU'!$V:$V,'PO & SKU'!$B:$B,$A2,'PO & SKU'!$U:$U,"=D")+SUMIFS('P O & SKU'!$V:$V,'PO & SKU'!$B:$B,$A2,'PO & SKU'!$U:$U,"=P")-SUMIFS('P O & SKU'!$V:$V,'PO & SKU'!$B:$B,$A2,'PO & SKU'!$U:$U,"=O")+$J2)/(SUM IF('PO & SKU'!$B:$B,$A2,'PO & SKU'!$G:$G)),2),IF(O2="MUL TISKU",IF( COUNTIF('P O & SKU'!$B:$B,'CONTAINER DATA'!$A4412)=COUNTIFS('PO & SKU'!$K:$K,"<>",'PO & SKU'!$B:$B,'CONTAINER DATA'!$A4412),1,(COUNTIFS( 'PO & SKU'!$K:$K,"<>",'PO & SKU'!$B:$B,'CONTAINER DATA'!$A4412)/COUNTIF('PO & SKU'!$B:$B,'CONTAINER DATA'!$A4412))),"")))

VBA

ActiveCell.FormulaR1C1 = _

"=IF(AND(LEN(RC[4])>2,IFERROR(MAX(VL OOKUP(RC1, Packing_Sl ip,11,FALS E),VLOOKUP (RC1,Packi ng_Slip,15 ,FALSE),VL OOKUP(RC1, Packing_Sl ip,19,FALS E)),0)>0), IF(AND(COU NTIFS('PO & SKU'!C4,""=MULTI*"")>0,ISN A(VLOOKUP( RC1,Packin g_Slip,8,F ALSE))=FAL SE),1,"""" ),IF(AND(R C10>0,RC[4 ]<>""MULTI SKU""),ROU NDUP((SUMI FS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=S"")+SUMIFS('P O & SKU" & _

"O & SKU'!C2,RC1,'PO & SKU'!C21,""=D"")+SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=P"")-SUMIFS('P O & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=O"")+RC10)/(SU MIF('PO & SKU'!C2,RC1,'PO & SKU'!C7)),2),IF(RC[4]=""MU LTISKU"",I F(COUNTIF( 'PO & SKU'!C2,'CONTAINER DATA'!R[4410]C1)=COUNTIFS( 'PO & SKU'!C11,""<>"",'PO & SKU'!C2,'CONTAINER DATA'!R[4410]C1),1,(CO" & _

"O & SKU'!C11,""<>"",'PO & SKU'!C2,'CONTAINER DATA'!R[4410]C1)/COUNTIF('PO & SKU'!C2,'CONTAINER DATA'!R[4410]C1))),""""))) "

Error:

"Application Defined or Object Defined error"

Thanks again!

It's the same problem I pointed out originally. Look at the text each side of the first line continuation character:

SUMIFS('PO & SKU" & _

"O & SKU'!C2

which will become:

SUMIFS('**PO & SKUO & SKU**'!C2

in your formula. You've duplicated part of the sheet name.

SUMIFS('PO & SKU" & _

"O & SKU'!C2

which will become:

SUMIFS('

in your formula. You've duplicated part of the sheet name.

Thanks! I went through and got it to this level. (still error) I have checked this/compared this with actual formula few times and still going through. Let me know if I am doing anything wrong.

ActiveCell.FormulaR1C1 = _

"=IF(AND(LEN(RC[4])>2,IFERROR(MAX(VL OOKUP(RC1, Packing_Sl ip,11,FALS E),VLOOKUP (RC1,Packi ng_Slip,15 ,FALSE),VL OOKUP(RC1, Packing_Sl ip,19,FALS E)),0)>0), " & _

"IF(AND(COUNTIFS('PO & SKU'!C4,""=MULTI*"")>0,ISNA(VLOOKUP( RC1,Packin g_Slip,8,F ALSE))=FAL SE),1,"""" ),IF(AND(R C10>0,RC[4 ]<>""MULTI SKU"")," & _

"ROUNDUP((SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=S"")+SUMIFS('PO & SKU'!C2,RC1,'PO & SKU'!C21,""=D"")+" & _

"SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=P"")-SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=O"")+RC10)/" & _

"(SUMIF('PO & SKU'!C2,RC1,'PO & SKU'!C7)),2),IF(RC[4]=""MULTISKU"",I F(COUNTIF( 'PO & SKU'!C2,'CONTAINER DATA'!R[2]C1)=" & _

"COUNTIFS('PO & SKU'!C11,""<>"",'PO & SKU'!C2,'CONTAINER DATA'!R[2]C1),1,COUNTIFS('PO & SKU'!C11,""<>"",'PO & SKU'!C2,'CONTAINER DATA'!R[2]C1)/" & _

"COUNTIF('PO & SKU'!C2,'CONTAINER DATA'!R[2]C1))),"""")))"

I'm not 100% sure as your original formula appears to have one too many parentheses to me, but perhaps:

```
ActiveCell.FormulaR1C1 = "=IF(AND(LEN(RC[4])>2,IFERROR(MAX(VLOOKUP(RC1,packing_slip,11,FALSE),VLOOKUP(RC1,packing_slip,15,FALSE)," & _
"VLOOKUP(RC1,packing_slip,19,FALSE)),0)>0),IF(AND(COUNTIFS('PO & SKU'!C4,""=MULTI*"")>0," & _
"ISNA(VLOOKUP(RC1,packing_slip,8,FALSE))=FALSE),1,""""),IF(AND(RC10>0,RC[4]<>""MULTISKU"")," & _
"ROUNDUP((SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=S"")+SUMIFS('PO & SKU'!C22," & _
"'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=D"")+SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=P"")" & _
"-SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=O"")+RC10)/(SUMIF('PO & SKU'!C2,RC1,'PO & SKU'!C7)),2)," & _
"IF(RC[4]=""MULTISKU"",IF(COUNTIF('PO & SKU'!C2,'CONTAINER DATA'!R[2]C1)=COUNTIFS('PO & SKU'!C11,""<>"",'PO & SKU'!C2," & _
"'CONTAINER DATA'!R[2]C1),1,COUNTIFS('PO & SKU'!C11,""<>"",'PO & SKU'!C2,'CONTAINER DATA'!R[2]C1)/" & _
"COUNTIF('PO & SKU'!C2,'CONTAINER DATA'!R[2]C1)),"""")))"
```

Or perhaps, a little shorter:

```
ActiveCell.FormulaR1C1 = "=IF(AND(LEN(RC[4])>2,IFERROR(MAX(VLOOKUP(RC1,packing_slip,11,FALSE),VLOOKUP(RC1,packing_slip,15,FALSE)," & _
"VLOOKUP(RC1,packing_slip,19,FALSE)),0)>0),IF(AND(COUNTIFS('PO & SKU'!C4,""=MULTI*"")>0," & _
"ISNA(VLOOKUP(RC1,packing_slip,8,FALSE))=FALSE),1,""""),IF(AND(RC10>0,RC[4]<>""MULTISKU"")," & _
"ROUNDUP((SUM(SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,{""S"",""D"",""P""}))" & _
"-SUMIFS('PO & SKU'!C22,'PO & SKU'!C2,RC1,'PO & SKU'!C21,""=O"")+RC10)/(SUMIF('PO & SKU'!C2,RC1,'PO & SKU'!C7)),2)," & _
"IF(RC[4]=""MULTISKU"",IF(COUNTIF('PO & SKU'!C2,'CONTAINER DATA'!R[2]C1)=COUNTIFS('PO & SKU'!C11,""<>"",'PO & SKU'!C2," & _
"'CONTAINER DATA'!R[2]C1),1,COUNTIFS('PO & SKU'!C11,""<>"",'PO & SKU'!C2,'CONTAINER DATA'!R[2]C1)/COUNTIF('PO & SKU'!C2," & _
"'CONTAINER DATA'!R[2]C1)),"""")))"
```

Wow it works! Spent countless hours on my end. Do you know what was the problem?

You had a misplaced parenthesis and one of your SUMIFS formulas was missing the data range for the first argument.

gah! The formula still worked perfectly. I guess the way VBA breaks up the larger formulas plays a role.

I ll make sure I don't go this big next time.

Thanks and have a nice weekend!

I ll make sure I don't go this big next time.

Thanks and have a nice weekend!