Need some help converting VBA formula to Evaluate ...

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
I have a couple of VBA formulas that are causing me distress ...

VBA Code:
    Part1 = "INDEX(Sheet2!$C$7:$C$" & LastRowSheet2 & ",AGGREGATE(15,6,ROW($A$1:$A$" & LastRowSheet2 & ")/(RIGHT(Sheet2!$C$7:$C$" & LastRowSheet2 & ",5)=""Total""),ROWS($1:1))),""-Total"","""""
    Range("D2").Formula = "=IFERROR(IFERROR(VALUE(SUBSTITUTE(" & Part1 & ")),SUBSTITUTE(" & Part1 & ")),"""")"                                                                  ' works
''    Range("D2").Value = Evaluate("IFERROR(IFERROR(VALUE(SUBSTITUTE(" & Part1 & ")),SUBSTITUTE(" & Part1 & ")),"""")")                                                           ' gives #VALUE! error

&

VBA Code:
    Range("S2").Formula = "=IF(OFFSET($N$1,ROW()-1,MATCH((S$1*2),$N$1:$R$1,0)-1)=0,SUMPRODUCT(--('LastRowSheet2'!$A$7:$A$" & LastRowSheet2 & "='Sheet3'!$A2)*('Sheet2'!$C$7:$C$" & LastRowSheet2 & "=$D2)*('LastRowSheet2'!$I$7:$I$" & LastRowSheet2 & "=S$1*2)*('LastRowSheet2'!$L$7:$L$" & LastRowSheet2 & ")),0)"         ' works
''    Range("S2").Value = Evaluate("IF(OFFSET($N$1,ROW()-1,MATCH((S$1*2),$N$1:$R$1,0)-1)=0,SUMPRODUCT(--('Sheet2'!$A$7:$A$" & LastRowSheet2 & "='Sheet3'!$A2)*('Sheet2'!$C$7:$C$" & LastRowSheet2 & "=$D2)*('Sheet2'!$I$7:$I$" & LastRowSheet2 & "=S$1*2)*('Sheet2'!$L$7:$L$" & LastRowSheet2 & ")),0)")  ' gives #VALUE! error

The formulas work fine, but when I try to convert the VBA formulas to Evaluate, the evaluation gives error #VALUE!.


After the evaluation problem is solved, I will need to put the evaluation results down the range, for example D2:D2000 for the first formula.
 
Last edited:
There is indeed a character limit of 255 for the evaluate, but I don't think that is the main issue here.

I renamed the variable 'LastRowSheet2' to 'LastRow' & that makes the second evaluate about 235 characters in length, still same result. :(

The second formula/evaluate is the one I am most concerned about because it is volatile and uses SUMPRODUCT on a large range instead of SUMIFS. Due to the number of formulas involved, the script is taking over a minute just to process all of the formulas.

I stink at formulas but I took a shot at using INDEX & SUMIFS to convert the second formula, It seems to give the same result, but I am not sure it would in other examples:
VBA Code:
        .Range("S3").Formula = "=IF(INDEX($N$1:$R$" & LastRow & ",ROW(),MATCH((S$1*2),$N$1:$R$1,0)-1)=0,SUMIFS('Sheet2'!$L$7:$L$" & _
                LastRow & ",'Sheet2'!$A$7:$A$" & LastRow & ",'Sheet1'!$A3,'Sheet2'!$C$7:$C$" & LastRow & ",'Sheet1'!$D3,'Sheet2'!$I$7:$I$" & _
                LastRow & ",'Sheet1'!S$1*2),0)"
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Strangely enough, for me anyways, with the converted second formula I came up with, The following 'Evaluate' code seems to give the same result as the formula:
VBA Code:
        MyFormula = "=IF(INDEX($N$1:$R$" & LastRow & ",ROW(),MATCH((S$1*2),$N$1:$R$1,0)-1)=0,SUMIFS('Sheet2'!$L$7:$L$" & _
                LastRow & ",'Sheet2'!$A$7:$A$" & LastRow & ",'Sheet1'!$A3,'Sheet2'!$C$7:$C$" & LastRow & ",'Sheet1'!$D3,'Sheet2'!$I$7:$I$" & _
                LastRow & ",'Sheet1'!S$1*2),0)"                                                                                                 ' works
        .Range("S4").Value = Evaluate(MyFormula)

Now I really am wondering if the conversion is correct. :rolleyes:
 
Upvote 0

Forum statistics

Threads
1,214,414
Messages
6,119,375
Members
448,888
Latest member
Arle8907

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