Executing a Command After Concatenating A String - vba

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.

Related

How to Translaste this =SUM(COUNTIFS(F4:AN4,{"0","1"})) into VBA?

I need help translating a common Excel function, into VBA code.
Please see attached screenshot for the code I already have started.
I am using the calculations seen in the screenshot to build a scorecard/grading worksheet. I will need to adjust the rows in each of these, but never the columns.
Once I figure this out, I will then loop these to repeat for each new row as they are added.
image of my code, so far
When a literal string needs to contain double-quote characters, you need to use two double-quotes in a row for each double-quote you need in the string.
So your string
Range("AP4").Formula = "SUM(COUNTIFS(J3:AR3,{"0","1"}))"
needs to look like this:
Range("AP4").Formula = "SUM(COUNTIFS(J3:AR3,{"“0"”,""1""}))"
You can also do this without putting the formula into the content of the cell like this:
Range("AP4") = WorksheetFunction.SUM(WorksheetFunction.COUNTIFS(J3:AR3,{""0"",""1""}))

Why does Excel stop a macro when using Application.Evaluate?

I'm trying to use the Application.Evaluate function to test if a conditional formatting condition is true. However, what is happening is that the macro just stops - no error message, and the cell in which the UDF is referenced returns #VALUE.
The value of the conditional formula Formula1 property in this instance is "=A1<>VLOOKUP($A1,actWOrders1!$A:$EF,COLUMN(A1),FALSE)"
I've tried replacing Application.Evaluate with ActiveWorksheet.Evaluate, in case it is the Application form is struggling with the context, but the same happens.
Any ideas what might be causing the issue?
Thanks! Screwtape.
If your macro doesn't contain any on error handing statements it would certainly provide you with an error message if at any point it was unable to execute. It could be that your macro is executing completely however is not achieving the result you expect.
Try running it step by step using F8 and observing what is happening on each line to find the culprit, as you step through you can hover your cursor over a number of items such as variables to see what their value is. You might also find reading this webpage will help you to use the tools in VBA to debug your macros.
You created a circular reference.
If you try and type the formula into a cell Excel will tell you that has a circular reference.
You can also step through a formula in the formula bar.
Highlight an expression in the formula that you want the value of
Press [F9] to calculate the expression
The expression will be replaced with its value

Excel VBA UDF that return value will override itself

Can I write a UDF in Excel VBA where the return value from the function will override the cell value from it is called from?
The function get information with a sql request. In this case it's only master data for example the item description. If the user will use this function in a worksheet in many cells excel will recalculate the cell value every time you change something. This has poor performance and normally it's only necessary to get the information one time and it hasn't to be updated in this case.
I thought to use application.caller.address method to get the address the function was called from but it seems it can't set the cell value for this address within the function.
So the return value of the function should override the original formula that run the function.
Is this possible
thanks for your help
No.
As you may have noticed Excel cells have multiple layers.
One is the "value". Another one the formula you can assign.
A funtions returns a value, therefore the return value only accesses this layer. So you cannot return a replacement for the formula cause it is on another layer.
A function differs from a sub in the return value, a sub does not return anything. Due to your behaviour of "one time usage" a sub will fit your need more than a function, because you dont want to return a value but to remove or replace certain content from cell (the formula).
However, this does not mean you cannot do this with a function - but still not with a return value. But you need to rewrite the whole formula on a data refresh if you would use such a function.
You may have missed a point that you make you laugh yourself I guess. Excel has such a thing natively. But it is not a function.
Copy your cells and paste them but use "values only".
Totally has the same effect.
Also in terms of recalculation... why not turn it off?
This would you not make to rewrite the function each time.

Range(...).Formula does not translate fully

I cannot figure this one out.
We use mostly french-version Excel (as we live in a french-speaking province of Canada). Somewhere in VBA code I set a cell's formula directly. Normally, we have to write the formula in english and Excel does the translation (writing the formula in any other language than english in VBA results in an error as far as I know). However, only HALF of this equation is translated which I think is causing me issues (writing the correct formula in another cell yields different results and most probably right results).
range("J2").Formula = "=round(IF(F2="",0,F2),2)-round(IF(G2="",0,G2),2)"
Is translated to this in the cell:
=ARRONDI(SI(F2=",0,F2),2)-round(IF(G2=",0,G2),2)
As you can see, the right part should read "ARRONDI(SI(.." but it does not read that way. I have tried adding spaces, removing the minus sign altogether, etc. Nothing works, it's always half translated. Any idea ?
In VBA you neexd to escape your quotations like this:
range("J2").Formula = "=round(IF(F2="""",0,F2),2)-round(IF(G2="""",0,G2),2)"
This is because the " Character is used in VBA as the start / end of a string. So if you want ot include it IN a string you need to type it twice in a row.

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.