Output of mathematical function in vba - vba

I am a complete novice when it comes to vba but I want to help a friend out, so I could use some guidance with a problem. I figure something like this is on the internet already, but I cannot find it, so if you have a suggestion as to where I could look, I would appreciate it.
I have a vba project with 19 modules, which compiles with no errors or warnings. The program calculates economic rate of return, given certain events. All the results come out as they should, apart from a single one. I am all but certain, that there is a mathematical flaw with this particular input, but I cannot locate it. This leads me to my question:
How can I obtain output from a (mathematical) function defined in vba? Preferably for discrete values of time, but at this point any help will do.

Simply call it. You can do it from a cell in a spreadsheet:
Assuming the function is called myMathematicalFunction and takes one parameter:
A1:
=myMathematicalFunction(B1)
B1:
<input value to test>

Related

EPPlus - custom function in C# - handling cell dependencies outside of the spreadsheet

What i'm doing: added a custom function to the EPPlus runtime. This function takes a text expression as an argument (of course the text expression can be specified as an argument or can be put in some cell of the spreadsheet that we refer)
So far, so good
then we evaluate this expression outside of Excel/EPPlus - the expressions can access data outside the spreadsheet BUT ALSO can refer to other spreadsheet cells.
And there is problem with referring back to the spreadsheet - EPPlus has no idea what's in these expressions and what will be the result of their evaluation, also doesnt know if they refer to other cells that may or may not be already calculated. So - no dependency control and possible mess.
Question: can this be fixed? I mean, when the expression evaluating function detects that the expression refers to a spreadsheet cell, can it tell EPPlus to evaluate that cell first? Or at least parse the expression and detect what are the dependencies, then give this information somehow to EPPlus so it knows in what order to run the calculations?
=MYEVAL("doSomeStuff('arg 1', Sheet.E4, Sheet.C4)")
MYEVAL is going to be run during Worksheet.Calculate() call - but inside MYEVAL, how to tell EPPlus that we want the current value of E4 and C4 cells, if these cells are calculated themselves and can change their value during calculation?
I dont care about cyclic references, can make sure there are no cycles, just the straightforward case of running the calculations in correct order.
Update: just wondering, if we can add any number of arguments to the call, could we just put our dependencies in the arguments list, like this:
=MYEVAL("doSomeStuff('arg 1', Sheet.E4, Sheet.C4)";E4;C4)
Then EPPlus would know that before calling MYEVAL it has to calculate E4 and C4 cells. Is this enough for enforcing such dependencies?
OK the solution presented in the Update is OK/useful and appears to work.
But there is also a very simple approach, alternative: you can calculate individual cells using the EPPlus api (Worksheet.Cells[i, j].Calculate()) - then you dont need to manage the dependencies, just call Calculate on the cells you need and this will follow actual dependencies.

Executing a Command After Concatenating A String

I am hoping to get some help on a formula I've been banging my head against the wall over. Essentially I am using Concatenate to produce a formula, when this is then copied into the final cell, it won't execute. I've tried using the hidden Eval function but that doesn't work for this specific part, however, it does for the other. The only thing that seems to solve it is either pressing Enter on each cell, or using the Find/Replace function. However, I need this to happen automatically. I've recorded using Find/Replace without any luck. The displayed color aspect is a custom function that works perfectly. I have had to change some of the wording for sensitivity reasons.
Concatenate Part:
=CONCATENATE("=IF(DisplayedColor(",I3,")=38,",eval(I3)*100,",""",E3,")")
Result to Execute:
=IF(DisplayedColor('[Spreadsheet]Tab1'!$BV$19)=38,-6.43801947500129,"")
Use the Indirect Function as follows:
Formula = INDIRECT(CONCATENATE("=IF(DisplayedColor(",I3,")=38,",eval(I3)*100,",""",E3,")"))
The INDIRECT function is useful when you want to return a value, based on a text string.

Word VBA - Load part of doc into variable, and run .indexOf to search

Okay, I am a Javascript programmer and VBA is driving me insane - I know nothing about it and it is like pulling teeth to find simple documentation on the simplest thing.
I'm literally trying to run a little script to auto-format a document, partly based in content.
I want to grab the third line of document, or first 100 characters, I really don't care, and run the equivalent of String().indexOf('foobar') on it, to check if that part of the document contains a string.
I cannot for the life of me find how to:
a. Load text selection into a variable.
b. Run a sane semblance to indexOf.
Can someone please help? And maybe point me to a sane VBA documentation that is not Micrsoft?
a. Load text selection into a variable.
Identify a range (Word.Range) you want and get the Text property of that range.
For instance,
dim s as string
s = ThisDocument.Paragraphs(3).Range.Text
b. Run a sane semblance to indexOf.
Is InStr no good?
msgbox InStr(s, "foobar")

Spline interpolation in Excel

I'll make this clear and simple, since I'm on a deadline here. Doing some reports for my section, and I came to a part where I could really use an interpolation in Excel.
Now, I've never had any practice with its functions, or VBA in general. So, is there a place where I could, or do anyone of you guys know some, ... where I could pick up a simple spline interpolation along with a tutorial of "how to make it work" (literally, I have no idea - I use Excel on a one-handful-of-times-on-an-annual-basis)?
I just need something that will put up a spline (or something similar) on an x and y column of values, and give me the interpolated result given values of X.
p.s. I know however of trendlines, but they're no use for me in this case. I cannot get the coefficients "programmatically" ...
I'd really appreciate some pragmatical help.
http://www.ozgrid.com/Excel/excel-interpolate-cubic-curve-fit.htm
XlXtrFun.xll is what I use to get splines in Excel.
This is a free add-in for Excel that adds spline and linear interpolation workbook functions:
http://www.srs1software.com/SRS1CubicSplineForExcel.aspx

Calculate a percentile using EPPlus

I'm trying to use either PERCENTILE.EXC, PERCENTILE.INC or PERCENTILE.
Looking at FormulaParserManager.GetImplementedFunctionNames() these are not implemented functions.
I wondered if I could set the formula and leave it to Excel to calculate. So far I've not got this to work and I get a #NAME? and "The formula contains unrecognized text". Merely clicking in the formula bar causes the formula to be calculated correctly.
Inspecting the internals of the Excel file I am creating (via EPPlus):
_xludf.PERCENTILE.EXC(B14:B113,0.95)
whereas in Excel I get:
_xlfn.PERCENTILE.EXC(A14:A113,0.95)
I think this is user defined function vs function. I've tried prefixing "_xlfn." to my formula string.
This is as far as I've got I think I either need to roll my own percentile calculation in code or manipulate the xml in the Excel file maybe.
Any help appreciated.
Doh! I spend all afternoon stuck, post a question here and then immediately suss the answer...
Anyway in case anyone is interested:
var row95 = percentile95RowLookup[profileKey];
var percentileRange = sheet.Cells[row, i, row+ profileCollection.Profiles.Count-1, i];
sheet.Cells[row95, i].Formula = $"_xlfn.PERCENTILE.INC({percentileRange.Address},0.95)";
With the important proviso that workbook.CalcMode is not Manual.