Using Evaluate method in VBA to write the formula result to worksheet cell

MahTah

New Member
Joined
Feb 26, 2016
Messages
12
I can use "Evaluate" method to write the formula's result into a worksheet cell for a function like:

ActiveSheet.Range("C27") = Evaluate("MAX('Test Data'!K:K)")

which works fine and I get the value in the cell "C27"

but when I try to use it for a formula like this:

ActiveSheet.Range("C16").Value = Evaluate("AVERAGEIF('Test Data'!B:B; MIN('Test Data'!B:B); 'Test Data'!I:I)")
OR
ActiveSheet.Range("C21") = Evaluate("=SLOPE('Test Data'!I:I;'Test Data'!D:D)")


I get "#VALUE!" as the result of evaluate method in cell "C16". This is while the formula itself works fine if I use it as a worksheet formula in the cell rather than using it in VBA!

1. What am I doing wrong here?
2. Beside this problem, how should I use the evaluate method with an array function?

Any help would be appreciated!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I think it is locale specific argument separator which is causing the issue. Make following changes to your evaluate code and test. Change the semicolon to comma and see.
ActiveSheet.Range("C16").Value = Evaluate("AVERAGEIF('Test Data'!B:B, MIN('Test Data'!B:B), 'Test Data'!I:I)")
OR
ActiveSheet.Range("C21") = Evaluate("=SLOPE('Test Data'!I:I,'Test Data'!D:D)")

Evaluate function can handle array functions (CSE in user interface). No different setting is required as far as I know.
 
Upvote 0
Thanks taurean,
Changing the semicolons to comma worked for those two functions but I still get the same "#VALUE!" result for a function array like:

ActiveSheet.Range("I16").Value = Evaluate("=AVERAGE(IF('Test Data'!B:B=MIN('Test Data'!B:B), IF('Test Data'!I:I<>"",'Test Data'!D:D)))")

Do you see any point also here!?
 
Upvote 0
Thanks taurean,
Changing the semicolons to comma worked for those two functions but I still get the same "#VALUE!" result for a function array like:

ActiveSheet.Range("I16").Value = Evaluate("=AVERAGE(IF('Test Data'!B:B=MIN('Test Data'!B:B), IF('Test Data'!I:I<>"",'Test Data'!D:D)))")

Do you see any point also here!?
 
Upvote 0
Thanks taurean,
Changing the semicolons to comma worked for those two functions but I still get the same "#VALUE!" result for a function array like:

ActiveSheet.Range("I16").Value = Evaluate("=AVERAGE(IF('Test Data'!B:B=MIN('Test Data'!B:B), IF('Test Data'!I:I<>"",'Test Data'!D:D)))")

Do you see any point also here!?
Yes. This formula unlike previous ones contains double quotes ("") which can make situation tricky. Wrap the double quotes in a pair like below and test.
Code:
ActiveSheet.Range("I16").Value = Evaluate("=AVERAGE(IF('Test Data'!B:B=MIN('Test Data'!B:B), IF('Test Data'!I:I<>"""",'Test Data'!D:D)))")
Evaluate function does not raise a runtime error but just returns result albeit erroneous.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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