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.
Related
I have a dropdown in i7. In j7 I have a formula, that adjusts the hyperlink -- based on value in i7. HLinks are to different cells in the same worksheet.
Trying to get XL to automatically jump to j7 upon value change in i7, and to follow/execute the corresponding HLink, meaning for the j7 to act as if it was clicked on (but without the use of sendkeys-left mouse click).
So far either line of below code, executed one at a time - on j7, gives "Run-time error '9': Subscript out of range"
Sub HLink_follow()
ActiveCell.Hyperlinks(1).Follow
ActiveWorkbook.FollowHyperlink ActiveCell.Hyperlinks(1).Address
End Sub
Am aware that this all can be done via VBA, without even having j7, but want to keep it the way that it is.
If you are not clear on something, ask a question.
When you use the Hyperlink-function in a formula, it doesn't add an entry to the hyperlink-collection of a cell. With other words, ActiveCell.Hyperlinks.Count is 0 and ActiveCell.Hyperlinks(1) therefore gives an Subscript out of range.
There is a question here on SO about that, have a look to this answer: https://stackoverflow.com/a/40343924/7599798. It suggests to parse the formula (split it by quote character) to get the URL and use FollowHyperlink with the extracted URL.
Now your case is more complicated as your cell doesn't contain only a simple =Hyperlink(..)-formula and therefore parsing is not an option. I see 2 possible solutions:
a) Instead of using a formula, add the Hyperlink via the Link-menu in Excel or the Worksheet.Hyperlink.Add method. Then add the logic to change the address in the Change-Trigger of your worksheet whenever some relevant data is changed.
b) Use a helper cell to calculate the destination of the hyperlink. The helper cell (let's say H7) would get a formula like IF(COUNTIF(I7,"x1"),"#A591",IF(COUNTIF(I7,"x2"),"#A665")). Your HLink_follow-routine would use the calculated address of that helper cell while the formula in J7 would have the simple formula =Hyperlink(H7, "See pic")
P.S. Yes, I am located in Europe - just look at my profile
Private Sub Worksheet_Change(ByVal Target As Range)
'auto selects cell
Dim MyVariable As String
MyVariable = Range("k8").Value
Application.Goto Reference:=Range(MyVariable)
End Sub
K8 contains a substitute formula to strip "#" from K7.
Works as expected.
How can I change formatting of the cell that uses vba function from the code of that function?
Example I tried:
made vba module (see code below)
put in excel sheet in some cell "=test()"
function "works" - it changes cell value and shows 2 popup windows. But formatting stays the same
Function test()
MsgBox (Application.ThisCell.NumberFormat) ' shows "General"
Application.ThisCell.NumberFormat = "Currency"
'Application.ThisCell.NumberFormat = "#,##0_);[Red](#,##0)"
MsgBox (Application.ThisCell.NumberFormat) ' still shows "General"
test = 12345.6
End Function
How to make it work?
(I need custom formatting rule, not "currency", but custom rule (test example in commented line) doesn't work too)
Please don't kill me for this wacko solution :-)
I have created a function, resulting in the number 4096:
Function very_weird_UDF()
very_weird_UDF = 4096
End Function
Then I have created a conditional formatting rule, based on the present of that UDF (User-Defined Function) inside the formulatext of that cell, as you can see here:
Conditional formatting's formula: =FIND("very_weird_UDF",FORMULATEXT((B2)))
Screenshot of what it looks like:
For your information:
Cell "C3" contains the formula, the other cells in B2:C5 just contain the value 4096.
Cells E2:F5 contain the mentioned =Find(...) formula.
Oh, if you wonder why I use such a weird name for the UDF? Just imagine I used a simpler name, like 'a' or 'f' or something like that. That might highlight a lot of cells who don't use that formula (like a simple =If(...) or an =And(...) or ...
I have the following formula
=IF((GLOBAL_DATE-30)<G2,"1 Month",IF((GLOBAL_DATE-60)<G2,"2 Month",IF((GLOBAL_DATE-90)<G2,"3 Month","Older Than 3 Months")))
and I would like to write this into specific cells using the FormualR1C1 in VBA.
(the GLOBAL_DATE is a named cell on another sheet)
Thanks
Select the cell that has that formula. In the VBE, go to the Immediate Window and type
?Activecell.FormulaR1C1
and press enter. That will give you the R1C1 translation of your formula.
This is what I get using the Macro Recorder:
ActiveCell.FormulaR1C1 = _
"=IF((GLOBAL_DATE-30)<R[1]C[6],""1 Month"",IF((GLOBAL_DATE-60)<R[1]C[6],""2 Month"",IF((GLOBAL_DATE-90)<R[1]C[6],""3 Month"",""Older Than 3 Months"")))"
That is using relative cell addresses (R[1]C[6] is the cell one row below and 6 columns to the rigth from the ActiveCell. Alternatively, you can use absolute adresses by replacing R[1]C[6] by R2C7 (for row 2, column 7 = G2).
You can easily use VBA to translate those formulae that you entered in a sheet into a sytax that's suitable for VBA. I once wrote a sub for that purpose.
I have an add-in xla file that I use to store my regularly used VBA code. This function is stored in the add-in modules.
Public Function IsFormula(cell_ref As Range)
IsFormula = cell_ref.HasFormula
End Function
This correctly returns True or False if I type it into cell: =IsFormula(A1)
However, when I try to create a new formatting rule using the formula option, I get this error 'You cannot use references to other worksheets or workbooks for Conditional Formating criteria.' The error is not because of quotation marks.
There isn't exactly a clear question to be answered here, but if you want to format say all of the cells in Sheet1 that contain formulae then, in Sheet1 A1:
1) define a name (say ‘Formulaic’, with ‘Sheet1’ for “Scope” and =GET.CELL(48,A1) for “Refers to”.
2) Select Sheet1
3) Set required conditional format with “Use a formula to determine which cells to format” and =Formulaic in “Format values where this formula is true:”
The ‘type_num’ (eg 48 above) is described at http://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
In an excel cell, I've placed a simple formula
=C4
The cell typically displays the value of cell C4, but instead I want to see the linked cell ID instead, which in this case is "C4".
Is there a formula to show me this? like:
=SHOWCELL(C4)
The reason I need this instead of simply typing the value of "C4" into the cell, is so Excel will maintain the link to the correct cell even if rows are inserted/deleted, AND show me which cell is linked.
You should be able to use the Cell function.
In Excel, the Cell function can be used to retrieve information about a cell. This can include contents, formatting, size, etc.
=Cell("address", C4)
This displays $C$4.
When inserting a row before C4, it is changed to $C$5.
In case you do not want the $ signs, one way would be the Substitute function:
=Substitute( Cell("address", C4), "$", "" )
You can create your own User Defined Function to achieve this. I call it "CellReference".
Usage:
=CellReference(B6)
displays "B6"
To use it, launch VBA, insert a module, and then copy the below into the module:
Function CellReference(cell As range) As String
CellReference = cell.Address(0, 0, xlA1)
End Function