Selecting a specific formula - excel

I'm wondering if anyone knows of a way to get a cell to implement a specific formula based on the contents of another cell.
For example, if one cell contains "general" then a generic sum formula is applied to the cell, though if it contains "conditional" then a countif formula is applied. I can't do this through a general series of chained if statements as there are around 500 formulas that constantly get added and amended and the formula entry area is insufficient to hold them all.

I've figured it out using a UDF and evaluate.
Public Function ev(r As String) As Variant
Application.Volatile (True)
ev = Evaluate(Replace(r, "#", Application.Caller.Row))
End Function
Put a Vlookup inside the ev() call, and have the formulas with # replacing all the row references. Not sure if it is the best way but it got it working.

Related

I need a formula that finds a cell with a given value, range is two dimensional

So the formula that I need is basically the Find feature from the ribbon, but in an excel formula.
Somewhere on the page is a value. I need to be able to search the page and find out what row and column that value is in.
All of the solutions I've seen work from the assumption that I will be able to already know what row or column it is in and find it, searching a one dimensional Range. But now I've got a 2D Range, and all those formulas are returning #N/A.
I will only know two pieces of data going in.
the cell value will be on the sheet, once and only once
it will be somewhere in the range A1:ZZ3500
Some of these sheets are enormous, and using the find function each time is giving me carpal tunnel.
Use AGGREGATE:
ROW:
=AGGREGATE(15,7,ROW(A1:ZZ3500)/(A1:ZZ3500="value"),1)
COLUMN
=AGGREGATE(15,7,COLUMN(A1:ZZ3500)/(A1:ZZ3500="value"),1)
You could roll your own UDF. In a new VBA module:
Function findValue(searchTerm As String, searchRange As Range) As String
findValue = searchRange.Find(searchTerm).Address
End Function
Which can be used in a worksheet formula like =findValue("yourvalue", A1:ZZ3500)
That will spit out the cell reference like $B$2 which you can then use =Indirect() to deal with, if need be.

Finding a range and then implementing it in another formula

I am trying to use a variable range based on a pre-defined criteria. In my case I would like to find the range of the “AUD” cells in the table. I managed to get the beginning of the range thanks to:
=ADDRESS(1;MATCH("AUD";1:1;0))
And then I found the end of the range using a slightly modified above formula:
=ADDRESS(1;(MATCH("AUD";1:1;0)+(COUNTIF(1:1;"AUD"))-1))
Then I simply combined the results with the following formula:
=(B4&":"&C4)
And the achieved result was:
$B$1:$D$1
However, I am having difficulties implementing this result inside formulas in which range must be defined, which brings me to my following questions:
Is such kind of implementation possible in EXCEL, I suspect that the result is considered as a simple text and not actually a cell reference? Is there a way I can change that?
One step further, if we trim (for example from $B$1 to just $B) can we still make the formula working?
Due to the fact that to save space I will probably write all the above formulas inside one formula and I expect this formula to become huge, would it be possible to create a VBA public function which can store the range in a variable and then just refer this variable to the formula - for example, SUMIF("=audRefCell()";"AUD";2:2).
I would like to thank you in advance for the help!

Is there a formula to look inside another cell's formula?

What I am working on is a quality-assurance spreadsheet, designed to analyse and quality check another spreadsheet for any errors in it's output and it's formulas.
On that latter note, I want to know if there's a formula that can inspect a cell (which also contains another formula) and return true or false if it contains a certain string of text, that is PART OF THE FORMULA itself.
For example, say in Cell A1 the formula is: =CEILING.PRECISE(B4) and equals 3.
I want to find out if the formula contains the word "Precise", which would then return TRUE. Another example being to see if the formula contains the string "B4" which would also return TRUE.
Depending on what Excel version you use....
Excel 2013/2016:
Make use of the build-in =FORMULATEXT() function. The function will return the formula of the referenced cell as text string. As such you could nest this in an =IF(...) statement.
Lower Excel version:
Make use of an UDF to accomplish the same effect. This doesn't have to be complicated. A simple UDF like below does the job.
Function FORMULATEXT(CL As Range)
FORMULATEXT = CL.Formula
End Function
The Answer of JvdV is already giving you everything you need about transforming the formula in a string.
In addition to this you can also use the FIND function to find if the word "PRECISE" is inside your formula.
=IF(FIND("PRECISE";FORMULATEXT(A1);1)>0;"TRUE";"FALSE")
Of course finding if B4 is inside would just be a small change after.

Excel: Use an if statement to return blank value for formulae and charts

Is there a way to have an if function return a value that will be ignored by both average functions and charts?
In Gnumeric, an open-source, Excel-like program, you can have an if function return "" and the cell will appear empty. If you take the average of a bunch of such cells, some with returned values and some with returned "", the "" will be completely ignored. And if you create a chart with those cells as data points, cells with "" will not have a point plotted.
However, doing the same thing in Excel doesn't seem to work. I've selected the "Show empty cells as: Gaps" option (described here) but it doesn't work. I think this is because the cell isn't technically empty.
Answers to similar questions suggest using "na()" in the if statement, but this messes with the averaging functions.
Does anyone know of a solution?
Note: While this subject area has been addressed before, I don't think this is a duplicate. Here are some similar questions:
IF statement: how to leave cell blank if condition is false ("" does not work)
Leave a cell blank if condition is false
Creating a chart in Excel that ignores #N/A or blank cells
Perhaps you could keep the #N/As (for the chart) but instead of using AVERAGE on it's own you could use the array formula (assuming the values you want to average are in A1:A5):
=AVERAGE(IFNA(A1:A5,""))
Once you've entered the formula press Ctrl+Shift+Enter to evaluate it as an array formula.
I think you would have to use a slightly more complicated function to calculate the average.
There may be a better way, but this will work (in conjunction with the na() method you mentioned in your question.
=SUM(IF(ISERROR(AVG_RANGE),0,AVG_RANGE)) / SUM(IF(ISERROR(AVG_RANGE),0,1))
This is an array formula so you'll need to commit it with ctl-shift-enter

Conditional formatting using AND() function

I'm trying conditional formatting on a sheet. I need to fill the cells with a certain color according to the following conditional statement:
=AND((INDIRECT(ADDRESS(4;COLUMN()))>=INDIRECT(ADDRESS(ROW();4)));(INDIRECT(ADDRESS(4;COLUMN()))<=INDIRECT(ADDRESS(ROW();5))))
When I try the statements in the AND() function separately, they seem to work, but when I put them together in the function I don't see any formatting happening.
Here is some background:
Row 4 of the "current column" has a date (DATE1) in it. There are also dates on the D and E columns of the "current row" (DATE2 and DATE3). So, I would like to fill the cell with a color if DATE1 is between DATE2 and DATE3.
I cannot see why the formula is not working. Any help is much appreciated.
Update (Dec 13, 2011):
I implemented a function that I call from the cells I need this functionality. The function returns integer values. Then conditional formatting only uses the integers in the cells. This way, the conditional formatting is less complicated. I'm passing INDIRECT(ADDRESS(ROW();COLUMN())) into the function I implement. So, I have all the information I need when working on relative and/ or absolute cells. Would be great to know a simpler way to pass the current cell as range into the function.
Note: ActiveCell didn't seem to work for me. It uses the data from the cell which is selected by the time the function is run. That's not what I'm looking for. I could of course pass the cell itself (as in A4, B7, etc.) but I'm not sure if it really matters in terms of performance.
Thanks to all of you who responded to my question.
I was having the same problem with the AND() breaking the conditional formatting. I just happened to try treating the AND as multiplication, and it works! Remove the AND() function and just multiply your arguments. Excel will treat the booleans as 1 for true and 0 for false. I just tested this formula and it seems to work.
=(INDIRECT(ADDRESS(4,COLUMN()))>=INDIRECT(ADDRESS(ROW(),4)))*(INDIRECT(ADDRESS(4,COLUMN()))<=INDIRECT(ADDRESS(ROW(),5)))
You can use a much simpler formula. I just created a new workbook to test it.
Column A = Date1 | Column B = Date2 | Column C = Date3
Highlight Column A and enter the conditional formatting formula:
=AND(A1>B1,A1<C1)
I had a similar problem with a less complicated formula:
= If (x > A & x <= B)
and found that I could Remove the AND and join the two comparisons with +
= (x > A1) + (x <= B1) [without all the spaces]
Hope this helps others with less complex comparisons.
This is probably because of the column() and row() functions. I am not sure how they are applied in conditional formatting. Try creating a new column with the value from this formula and then use it for your formatting needs.
COLUMN() and ROW() won't work this way because they are applied to the cell that is calling them. In conditional formatting, you will have to be explicit instead of implicit.
For instance, if you want to use this conditional formating on a range begining on cell A1, you can try:
`COLUMN(A1)` and `ROW(A1)`
Excel will automatically adapt the conditional formating to the current cell.
I am currently responsible for an Excel application with a lot of legacy code. One of the slowest pieces of this code was looping through 500 Rows in 6 Columns, setting conditional formatting formulae for each. The formulae are to identify where the cell contents are non-blank but do not form part of a Named Range, therefore referring twice to the cell itself, originally written as:
=AND(COUNTIF(<rangename>,<cellref>)=0,<cellref><>"")
Obviously the overheads would be much reduced by updating all Cells in each Column (Range) at once. However, as noted above, using ADDRESS(ROW(),COLUMN(),n) does not work in this circumstance, i.e. this does not work:
=AND(COUNTIF(<rangename>,ADDRESS(ROW(),COLUMN(),1))=0,ADDRESS(ROW(),COLUMN(),1)<>"")
I experimented extensively with a blank workbook and could find no way around this, using various alternatives such as ISBLANK. In the end, to get around this, I created two User-Defined Functions (using a tip I found elsewhere on this site):
Public Function returnCellContent() As Variant
returnCellContent = Application.Caller.Value
End Function
Public Function Cell_HasContent() As Boolean
If Application.Caller.Value = "" Then
Cell_HasContent = False
Else
Cell_HasContent = True
End If
End Function
The conditional formula is now:
=AND(COUNTIF(<rangename>,returnCellContent()=0,Cell_HasContent())
which works fine.
This has sped the code up, in Excel 2010, from 5s to 1s. Because this code is run whenever data is loaded into the application, this saving is significant and noticeable to the user. It's also a lot cleaner and reusable.
I've taken the time to post this because I could not find any answers on this site or elsewhere that cover all of the circumstances, whilst I'm sure that there are others who could benefit from the above approach, potentially with much larger numbers of cells to update.
Same issues as others reported - using Excel 2016. Found that when applying conditional formulas against tables; AND, multiplying the conditions, and adding the conditions failed. Had to create the TRUE/FALSE logic myself:
=IF($C2="SomeText",0,1)+IF(INT($D2)>1000,0,1)=0

Resources