I need to have a capability to execute any kind of build-in functions (such as 'sum' or 'len') from VBA (MS Excel).
One of the restrictions that I have is that I may not pass cell ranges as arguments to these functions. Instead of this, I should be able to use strict values.
I want to be able to use the following expression SUM(1, 2) which should return 3, while the following version SUM("A1:A2") won't work for me.
I managed to develop some function that parses my prior input and makes it consist of a list of values (as for example above, it made the user's input of 'A1:A2' look like an array of numbers consisting of two values).
So, can anyone give me an example of using a build-in function that receives a list of values (not just range of cells)?
I tried the following code, but for some unknown reason, I haven't been able to get it working (I keep getting 1004 error, saying: Cannot run the macro 'SUM'. The macro may not be available in this workbook or all macros may be disabled.):
Application.Run "SUM", 2, 2
Some valuable advices that would help to find a solution to this problem will be greatly appreciated.
To use a built-in, Excel, Worksheet function, you need to do something like the following:
Application.WorksheetFunction.Sum(2,2)
Related
I am currently rewriting some old Access code that defined named ranges in an excel workbook.
The old code is as follow:
oWorkbook.Names.Add "NameOfRange", "='Sheet1'!$A$1:$L2000"
With oWorkbook a Workbook opened from Access.
Since the range (number of rows) can change, I planned to use dynamic named ranges using Offset and CountA functions, and wrote the following code to replace the previous one:
oWorkbook.Names.Add "NameOfRange", "=OFFSET('Sheet1'!$A$1,0,0,COUNTA('Sheet1'!$A:$A),12)"
but then excel send an error 1004 There is a problem with this formula.
The same line of code executed from an Excel workbook works fine, and give the expected result.
On my computer, I use mixed English/French international settings, and my Office 2016 is in English, so I have a ListSeparator as ;.
When replacing the , by ;, the new formula below works:
oWorkbook.Names.Add "NameOfRange", "=OFFSET('Sheet1'!$A$1;0;0;COUNTA('Sheet1'!$A:$A);12)"
but I need my function to work on every configuration, not on only French/English bizaroid settings.
I tried to specify the named arguments (Name:= ... and RefersTo:=...) but its not correcting the problem. When called from Access, the code still need local separators (and maybe local function names? no idea).
So I can retrieve the computer excel List Separator (Using oWorkbook.Application.International(xlListSeparator) and replace all , in the formula with it, but maybe I am doing something wrongly and there is a better way to do?
Some formulas I'm using only work in Microsoft Excel but not Google Sheets, or a different formula that has the exact same result works in Google Sheets but not Microsoft Excel.
Is there a simple way to detect if Google Sheets or Microsoft Excel is running, and execute the compatible formula accordingly?
I am finding it difficult to believe this question hasn't been asked before but I couldn't find my specific question of the same file working in both Excel and Google Sheets.
I'm not concerned about scripts, just formulas.
There is no official way to determine if Microsoft Excel or Google Sheets is running through formulas, however with some versions of Excel =INFO("RELEASE") will return the version of Excel running, but this doesn't work in Google Sheets.
Thanks to #Slai, =ISERR(-"1pm") results in TRUE in Excel and FALSE in Google Sheets. Excel doesn't understand how to apply math to a time string value, where Google Sheets does, hence the error.
A simple IF statement using this logic can be =IF(ISERR(-"1pm"),"MS-Excel_formula","G-Sheets_formula")
If the formula you're using returns a number, a more simplified version is =IFERROR(GoogleFormula+"0am", ExcelFormula) however this won't work for non-numeric returned formulas because it adds 0 to the result which should not change a numeric result. For instance, =IFERROR(1+"0am", 2) returns 1 in Google Sheets and 2 in Excel.
If Microsoft decides to recognise "1am" or "0am" as numeric time values in future versions of Excel, the above if statements won't work as intended and Excel will attempt to execute the Google Sheets formula.
One boolean solution is to force an error in a function you absolutely know doesn't work in one or the other program. So #Slai 's example would work well no? Also, excel doesn't attempt to implement the Google only functions like ArrayFormula or Importrange &c. FWIW The minor discrepancies are the ones that catch me out - 'DSUM' for example frustrates me in that Google Sheets responds slightly differently to the Excel implementation under certain conditions. There will be others I'm sure but I've not yet found a definitive list or collation thread of these. (PS. I've not expanded my description of the DSUM idiosyncrasy as I suspect it'll be too far off topic).
I have an excel sheet that contain multiple formulas. Formulas refer to cells from other sheets in same workbook.Is there any npm package that can evaluate formulas having references of cells from other sheets?
I am trying to do the same thing right now.
My attempt includes:
xlsx - to read values and functions from the file
hot-formula-parser - to evaluate functions
This library will fire an event each time it tries to evaluate a cell reference.
See callcellvalue documentation for more information
Also see callrangevalue documentation for more when range is required
This should suffice to implement the solution.
Let me know how it works and share your work :)
I will also share mine if I get it working.
Update
So turns out the library [hot-formula-parser] does not officially support cross sheet references. See more info at this ticket
However, I opened a PR for them to support it. Currently available at my fork
I am also working on a library that will enable you to point to a cell, and it will evaluate it. So even if it points to other cells which are formulas, it will recursively evaluate them. Let me know if it is of any interest to you.
Another Small Update
I was able to finish a POC that evaluates thousands of formulas along the way using the stack mentioned above and my PR.
I required some more patches along the way, but most of them can be worked-around if you can modify the excel file (for example, the name of the tab could not have spaces).
To extract the first coefficient of polynom which approximate a function, I use this formula:
=INDEX(DROITEREG(B2:B10;A2:A10^{1.2.3});1)
But when i want to use it as a VBA code like that :
a = INDEX(DROITEREG(B2:B10;A2:A10^{1.2.3});1)
I get a syntax error
Can someone help me to resolve this problem? Thank you
To use Excel formulas in VBA you must use Application.WorksheetFunction.<function name> as follows (if using inside of Excel, the Application. can be left out):
a = Application.WorksheetFunction.Index(DROITEREG(B2:B10;A2:A10^{1.2.3});1)
I was unsure how to implement the DROITEREG function and left it as is.
See MSDN for more info
A few questions have come up recently involving the Application.Evaluate method callable from Excel VBA. The old XLM macro language also exposes an EVALUATE() function. Both can be quite useful. Does anyone know why the evaluator that is exposed can handle general expressions, though?
My own hunch is that Excel needed to give people a way to get ranges from string addresses, and to get the value of named formulas, and just opening a portal to the expression evaluator was the easiest way. (The help for the VBA version does say its purpose it to "convert a Microsoft Excel name to an object or a value".) But of course you don't need the ability to evaluate arbitrary expressions just to do that. (That is, Excel could provide a Name.Evaluate method or something instead.)
Application.Evaluate seems kind of...unfinished. It's full behavior isn't very well documented, and there are quite a few quirks and limitations (as described by Charles Williams here: http://www.decisionmodels.com/calcsecretsh.htm) with what is exposed.
I suppose the answer could be simply "why not expose it?", but I'd be interested to know what design decisions led to this feature taking the form that it does. Failing that, I'd be interested to hear other hunches.
Well I think its required to enable VBA to get the result from a Named Formula (or a string containing a formula), (OK there is also the ugly method of inserting the formula into a spare cell and then reading back the result, but for example that won't work from inside a UDF).
In VBA its complex to determine if a Defined Name contains a range reference or a formula. Using Evaluate works for both cases.
Its also sometimes very efficient and simpler to build Excel formulae as strings and evaluate them rather than having to bring all the data from Excel into VBA and then do the calculations in VBA. (Its expensive to get data from Excel into VBA and even worse with current .NET implementations).