Ok, so I did have a quick search but did not find an answer that does what I would like... I have a horrible feeling that it is simple.. :)
I would like a formula in cell D1 that will use cells A1, B1 and C1 to evaluate the test shown in B1. This is for producing questions.
The picture shows the data and results.
I have tried indirect() with no success and &, ie
=A1&B1&C1
You can use the Evaluate method in excel VBA. Note that i'm using .Value on the rng2. This is because we want to use the displayed value of the cell (in the format in which it appears to the user lookins at the cell) rather than the underlying value that excel stores in the cell (which is what .value2 gives us).
Public Function EEE(rng As Range, rng2 As Range, rng3 As Range)
EEE = Evaluate(rng.Value2 & rng2.Value & rng3.Value2)
End Function
Note that there is a method of doing it using only worksheet functions, but it rather complicated compared to the VBA solution. An excellent descriptio of the old Evaluate() function and how to use it is given in this article.
Following formula could be used to evaluate the result of combination of A1, B1, C1
=ISLOGICAL(A1&B1&C1)
or
=ISLOGICAL(CONCATENATE(A1,B1,C1))
Related
I am trying to apply conditional formatting to a cell when it contains an equation with reference to a named cell.
Example: Cell I71 contains the following equation. =G71*Admin_Labor_Rate
Admin_Labor_Rate has a value of $50, and is the name of cell B152.
Now I want to find all cells that reference cell B152 "Admin_Labor_Rate" and apply conditional formatting (highlight). I have two other Labor Rates, so I want to use cell highlighting to distinctly identify if the labor is being performed by technicians, admin, or engineering.
I have done a lot of research and tried a lot of methods, but it seems as close as I can get is if the cell equates to a certain value, then I can format it.
Thanks in advance for any thoughts the community may have on this.
Use this formula with conditional formatting:
=ISNUMBER(SEARCH("Admin_Labor_Rate", FORMULATEXT(A1)))
Where A1 is the top-left cell of the range you want to conditionally format. For example, you select range F20:H30 and enter the formula with F20 instead of A1.
Perhaps a User Defined Function (i.e. UDF).
function formulaContains(rng as range, nr as string) as boolean
formulaContains= rng.hasformula and cbool(instr(1, rng.formula, nr, vbtextcompare))
end function
What formula do you use to check if another cell has formula? For example, I have 2 columns, A has cells which contains either a formula or a value.
(Column A usually contains Formulas but other users try to change their values by directly typing and replacing the formula that was previously there)
In Column B I want to add a formula that will say "HasFormula" if the cell on Column A has formula and say "PlainValue" if it contains a value.
I'm thinking maybe using =ISNUMBER() but that may not be accurate.
I am using Excel 2010.
Excel actually has a builtin ISFORMULA() function.
Say A1 has a formula and you want to check that. In say B1, you can use:
=If(ISFORMULA(A1),"HasFormula","PlainValue")
Edit: Per your comment, you don't have ISFORMULA(). An alternative is to create a quick UDF, and use the custom function in the worksheet.
In a workbook module, put this code:
Function isFormula(ByVal target As Range) As Boolean
isFormula = target.hasFormula
End Function
Then you can call it like this: =isFormula(A1) and it will return TRUE if A1 has a formula.
If you can't use VBA, then you can use this formula:
=IF(ISERROR(FORMULATEXT(A1)),"PlainText","HasFormula")
The MrExcel website (link below) has this method which uses old code from Excel 4 (which is still present for backward compatibility)...
Define a NAME such as "CellToLeftHasFormula" and in the "refers to" box put
=GET.CELL(48,OFFSET(INDIRECT("RC",FALSE),0,-1))
Then in column B use the formula =CellToLeftHasFormula which will return TRUE if it has.
Be aware that this will mean your Excel will now contain a macro and so will need to be saved as such (xlsm). I use this in Excel 2010.
For full explanation (and other .CELL options, besides 48) see MrExcel link: https://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
You can use the Range.HasFormula property.
https://learn.microsoft.com/en-us/office/vba/api/excel.range.hasformula
EDIT:
Text and code from the above link:
"True if all cells in the range contain formulas; False if none of the cells in the range contains a formula; null otherwise. Read-only Variant. ..."
Worksheets("Sheet1").Activate
Set rr = Application.InputBox( _
prompt:="Select a range on this worksheet", _
Type:=8)
If rr.HasFormula = True Then
MsgBox "Every cell in the selection contains a formula"
End If
You can restrict the user by protecting the column A.
You can directly check if a cell contains a formula by using a shortcut Ctrl + `.
You can use vba and write a user defined function :
1. Press alt + F11
2. Insert module in workbook
3. Paste this code
Function IsFormula(cell_ref As Range)
IsFormula = cell_ref.HasFormula
End Function
4. Now, use Isformula in the cell wherever you want.
I have a cell C1, where C1 =AVERAGE(E1:E10).
In cell D1 I would like to have D1 =STDEVP(E1:E10) without explicitly typing the range E1:E10 as I need to use this and other ranges multiple times.
Is there a simple way to get/refer to the affected range in C1 for use by another function in another cell? Something like D1 =STDEVP(AFFECTEDRANGE(C1)).
I found a function called INDIRECT which roughly does what I want, but it requires additional columns for my purpose. As I prefer to keep my worksheet clean and compact I'd prefer a function as described above. Does a one-liner like this exist?
A Replace on the Range.Formula property would seem to be sufficient.
range("d1").formula = replace(range("c1").formula, "AVERAGE", "STDEVP", vbtextcompare)
If you want to stay within the worksheet and avoid VBA, use named ranges.
Solved it: I merged the answer of Jeeped with the answer to this post to create a custom reusable function that does what I need.
It now works by setting =AverageToStDev(CELL) as a value for any cell. Note that CELL must be a single cell containing the AVERAGE() function for this to work.
Function AverageToStDev(MyCell As Range)
Application.Volatile
AverageToStDev = Evaluate(Replace(MyCell.Formula, "AVERAGE", "STDEVP", vbTextCompare))
End Function
I am trying to convert a text string into a formula, but my text string is a bit unusual because it describes a link between two worksheets. Here is the starting formula:
+TEXT("+'X:\Deals\ONE-PAGERS\_One-pagers (vM)\["&TRIM(LEFT(SUBSTITUTE($B9," ",REPT(" ",30)),64))&" vM.xlsx]Metrics'!$D$8",1)
I created this formula to avoid having to manually link a large number of spreadsheets. It captures the first two words of a cell, and places that into a text string that would establish a link, if it were a formula. This way, I can simply enter a company name into cell B9, and this formula will produce a text string that describes the link I want. Here is the formula output:
+'X:\Deals\ONE-PAGERS\_One-pagers (vM)\[xxx vM.xlsx]Metrics'!$D$8
As you can see, if this were a formula, it would generate a link it would return the value in Cell D8 of the xxx worksheet. I have tried the evaluate workaround (seen below) as well as the indirect function and neither seems to be working. Let me know if this doesn't make sense and thanks so much in advance for your help!
Function EvalCell(RefCell As String)
Application.Volatile
EvalCell = Evaluate(RefCell)
End Function
Evaluate and INDIRECT would both require the source workbook to be open, but you could use ExecuteExcel4Macro:
Function EvalCell(RefCell As String)
Application.Volatile True
application.ExecuteExcel4Macro(application.convertformula(mid$(RefCell, 2), xlA1, xlr1c1))
End Function
I'd avoid using too many of these functions though!
Is there any way to use index numbers to refer to specific cells, like how in VBA you can use Cells(3,2) to refer to cell C2.
I was hoping "=CELLS(3,2)" would work, but, sadly, that doesn't appear to exist. Is there another way to do that?
EDIT:
I should clarify that I need to use this inside of a SUM() worksheet function, so it would need to return the cell reference, not the value inside the cell.
You can use the OFFSET(original_range,rowsOffset,colOffset) formula to get a reference to a range which is some specific offset from a point on the sheet.
There are also two additional parameters you can pass to offset which determine #rows and #columns of the returned range:
=SUM(OFFSET(A1,0,0,12,1))
will give you a range 12 rows by 1 column starting at A1.
=SUM(OFFSET(A1,2,2,12,1)) would start at C3
Like doing Offset(r,c).Resize(12,1) in VBA
You could change the reference style... I often prefer the R1C1 reference style for certain functions I'm writing.
Here's a link that shows how to get to it:
http://www.excelqa.info/2010/12/06/switch-to-r1c1-reference-style-in-excel-2010/
You can just write a custom formula to handle that.
Public Function customFunction(a As Integer, b As Integer, Optional sh As String)
If sh <> "" Then
customFunction = Sheets(sh).Cells(a, b).Value
Else
customFunction = Cells(a, b).Value
End If
End Function