Home » excel » Use a string as a formulae in Excel VBA

Use a string as a formulae in Excel VBA

Posted by: admin May 14, 2020 Leave a comment


I am building up a formulae based on values set up ion a parameter sheet in the spreadsheet. I now want to vealuate the formulae in VBA but don’t know how to take the string and convert it into code instead of a string.

So the formulae is built up in code as follows (varies according to options, this is just a simple addition where the workbooks, sheets and cells are all variable for both terms)

CalculationHoldArray(Loopcount) = "'[" & Usefile1.Name & "]" & Worksheet1 & "'!" & Cells1 & ", + '[" & Usefile2.Name & "]" & Worksheet2 & "'!" & Cells2

The resulting string is in this case is

"'[Control Model v1.35- nonfunctional.xlsm]Input'!E2, + '[Control Model v1.35- nonfunctional.xlsm]Input'!E3"

How would I convert this to code? I have tried doing it as an application.worksheet.sum function, but that gives an error. In addition to the sum, I also need to be able to multiplication, division, subtraction and sumproduct for arrays. I thought that using application.worksheet.sum function would work, but I just get errors

How to&Answers:

Application.Evaluate("Sheet1!A1 + Sheet1!A2")

Note that there’s a limit of 255 characters for the string expression you’re evaluating.

Documentation on MSDN