Why does not field with custom function to get name recalculate? - excel

I use a vba function to get cell name (taken from Retrieving Cell name in Excel)
Public Function CellName(oCell As Range) As Variant
Dim oName As Name
For Each oName In ThisWorkbook.Names
If oName.RefersToRange.Parent Is oCell.Parent Then
If Not Intersect(oCell, oName.RefersToRange) Is Nothing Then
CellName = oName.Name
Exit Function
End If
End If
Next
CellName = CVErr(xlErrNA)
End Function
When the name of the cell does not exist, it shows error -- and that is of course intended behaviour. However, when I then name the other cell (the one which name I want to get), the error in my cell is still active. Recalculating does not help. I need then to change the value of the other cell (I can change its value or change value of yet another cell that is in its formula), or its formula, so the value in that cell would be recalculated, so my cell with CellName function gets properly refreshed.
I don't see the point why, and what can I do to simple make the cell refresh when I name the cell I point to?
This is Excel 2007, file type xlsm.

I think you need to set the function as Volatile
at the beginning of your UDF, add this code:
application.volatile
see MSDN Library: Volatile Method [Excel 2003 VBA Language Reference]

Related

Return the results of one cell containing a formula into another cell so that cell only shows a value

I am trying to code my spreadsheet to react to changes to a specific cell in my spreadsheet. This cell contains a formula so the programing is not recognizing any change to the cell although the number is updating the formula is not. I am looking for a way to return the results of the cell containing the formula into another cell as a value so the change can be recognized by the code.
The change event isn't firing because the contents of the cell aren't changing, just what it displays (the formula result) is. You could use the Worksheet_Calculate event and check the value against another static value. If it's changed, then update it and trigger your other code.
It sounds like there's a better way to design your sheet though.
Private Sub Worksheet_Calculate()
Dim watchCell As Range ' set to something
Dim checkCell As Range ' set to something
If checkCell.Value = watchCell.Value Then Exit Sub
' Value has changed. Update the check and trigger action.
checkCell.Value = watchCell.Value
Call SomeOtherResponse
End Sub

Macro added PrevSheet function returns #NAME error

I added a simple Previous Sheet macro to file and it worked normally, until it started to return #NAME error, and i can't figure out why.
I meant to use it with named single cell ranges which are consistent across the workbook, I'd used it successfully before naming the ranges and didn't think it would have any impact on the fuction. After naming the ranges though it no longer works, not even for regular non named ranges.
I have tested this by creating a new workbook, filling some sheets and trying it out, and it still returns a #NAME error. When i evaluate the function, the error appears at the very first step: recognizing the function. However, when i type into the formula bar, the programs offers me the formula normally.
I have also tried referring to the named cells by its cell, and even adding the worksheet name before the cell (eg "prevsheet(previoussheetname!a1), or prevsheet(thissheetname!a1)). I have even, in a last ditch effort, tried adding double quotes before the cell name.
For full disclosure, i have also another macro subroutine that uses references to previous and next sheets, but as it wouldnt recognize the function itself (which should have been an early sign), it makes use of relative referencing (ie activesheet(index - 1, activesheet(index + 1)). At the time i didn't think it would mess up the function, but as i grow ever more desperate and confused, maybe thats a possibility.
the PrevSheet() code i was using:
Function PrevSheet(RCell As Range)
Dim xIndex As Long
Application.Volatile
xIndex = RCell.Worksheet.Index
If xIndex > 1 Then _
PrevSheet = Worksheets(xIndex - 1).Range(RCell.Address)
End Function
And as it is now, as suggested by Chris Neilsen
Function PrevSheet(RCell As Range) As Variant
Application.Volatile
PrevSheet = RCell.Worksheet.Previous.Range(RCell.Address).Value
End Function
As suggested by Chris Neilsen i have edited the named ranges to look like this:
!(nothing)$column$row with its scope set to Workbook
The named range is not available at the range browser.
Only cell B1 is named. It is called "name"
PrevSheet() does not work with either range.
Macros are enabled
Anyone with a better understanding of vba, macros and excel can tell me why this is happening and how do i fix it so it returns the value of the specified cell in the first sheet to the left of sheet the function is typed in? (ie, in sheet4, =prevsheet(A1) will return the value of cell A1 in sheet3)
I hope my question is clearer now!
Your code appears to work if it is placed in a Standard Module:
Public Function PrevSheet(RCell As Range) As Variant
Dim xIndex As Long
Application.Volatile
xIndex = RCell.Worksheet.Index
MsgBox xIndex
If xIndex > 1 Then
PrevSheet = Worksheets(xIndex - 1).Range(RCell.Address)
End If
End Function
For example, in the worksheet:
I have assigned cell A7 the Name junk and the 666 is the value in the previous sheet's cell A7.
This will work, if you define your Named Ranges correctly. There are several ways this can be done, but here's one that is IMO simplest.
Since you say ...use it with named single cell ranges which are consistent across the workbook. you can create a single Named Range, Workbook scope, that will refer to a cell (or cells) on the sheet which references the name.
Lets say you want to refer to cell A1. In Name Manager, create a Name, lets say YourNamedRange workbook scope, Reference =!$A$1 (Note the ! without a sheet reference).
When you add a formula to a sheet (eg =YourNamedRange) it will refer to cell A1 on the sheet containing the formula.
Applying it to your UDF, just use =PrevSheet(YourNamedRange)
Your UDF works (mostly) as is, but will fail if a different Workbook is active. To fix that, use
Function PrevSheet(RCell As Range)
Dim xIndex As Long
Application.Volatile
xIndex = RCell.Worksheet.Index
If xIndex > 1 Then
With RCell.Worksheet.Parent 'The workbook containing RCell
PrevSheet = .Worksheets(xIndex - 1).Range(RCell.Address)
End With
End If
End Function
There is also a WorksheetProperty called Previous that does much the same thing, so you can refactor as
Function PrevSheet(RCell As Range) As Variant
Application.Volatile
PrevSheet = RCell.Worksheet.Previous.Range(RCell.Address).Value
End Function

Get the Name of a cell via its address or reference by using a function in EXCEL

I searched a lot but didn't found any good answers for it!
Is there any internal Function returning the Name of a cell by passing its address as a parameter?
if it's not, what is the simplest way to obtain the name we've defined for a cell via giving its address/reference ???
for example I defined 'test_name' as a name for cell B4 which its content is: 'test'.
I want a function in excel, like : CellName(adr) and using it like CellName(B4) and returns 'test_name'
I also searched a lot to find a build-in excel function, but not found anything!
you can use UDF (User Defined Function) to achieve a Cell Name in Excel
for this act, we're going to use visual-basic language (vb)
I found some good defined functions and I'll explain how to add.
I've appended 3 types of VB Codes you can choose one of them by your own and add it.
STEPS
FIRST, Opening the needed items: you can see this step here or follow bellow:
Open a new workbook in excel.
You should define the desired function in visual basic for applications (VBA)
For opening the VB use the short key alt + F11
Add a new Module to your workbook.
a new module window will be opened which is used to add your code there.
SECOND, Appending the code:
Copy just one of these three codes:
1. CellName1(cell): takes the cell address and returns the cell's name
Public Function CellName1(cel As Range) As Variant
Dim nm As name
For Each nm In Names
If nm.RefersTo = "=" & cel.Parent.name & "!" & cel.Address Then
CellName1 = nm.name
Exit Function
End If
Next
CellName1 = CVErr(xlErrNA)
End Function
2. CellName2(cell): takes the cell address and returns the cell's name
Function CellName2(cel As Range) As String
Dim rng As Range
On Error Resume Next
Set rng = cel
If Len(rng.name.name) < 0 Then
CellName2 = "No named Range"
Exit Function
End If
CellName2 = rng.name.name
If InStr("CellName2", "!") > 0 Then
CellName2 = Right(CellName2, Len(CellName2) - InStr(CellName2, "!"))
End If
End Function
3. CellName3(row,col): takes the cell row and column and returns the cell's name
Function CellName3(r As Long, c As Long) As String
Dim rng As Range
Set rng = Cells(r, c)
On Error Resume Next
If Len(rng.name.name) < 0 Then
CellName3 = "No named Range"
Exit Function
End If
CellName3 = rng.name.name
End Function
don't forget to save the function you've pasted!
Examples
consider cell B4 that has a name: "test_name", now you can get it's name by each functions defined above...
now assume another cell like D7.
we use the first function (CellName1) for this cell and for the result, the name of the 'B4' cell will be the content of 'D7'
so, we passed B4 to the function as the parameter and obtained its name as the content of 'D7' cell.
CellName1(B4)
or
CellName2(B4)
or
CellName3(4,2)

Can an Excel cell formula refer to a UserForm control?

I have a UserForm with text boxes that are already bound to certain worksheet cells through the ControlSource property. I need to run a calculation between two of these bound values and have the result end up in a third worksheet cell. I know there are numerous ways this can be done, but I'm wondering whether there's some way to do this as a formula in the worksheet cell that references the UserForm control values. For example, I would like to be able to put a formula in cell C3 that goes something like
= UserForm1.TextBox1.Value * UserForm1.TextBox2.Value
but I haven't found any reference that addresses using worksheet cell formulas to fetch values directly from UserForm controls. (And no, in this case I can't just reference the bound cells by plugging something like "= A1 * B2" into cell C3. This question is specifically about whether it's feasible to reference a UserForm control from within a worksheet cell formula.)
Thanks in advance for any helpful suggestions.
The only way to refer to the property of an ActiveX control on a worksheet, is via a user-defined-function, such as:
Public Function GetTextBoxValue(TextBoxName As String) As String
On Error GoTo 0
Dim o As OLEObject
Set o = Sheet1.OLEObjects(TextBoxName)
On Error Resume Next
If Not o Is Nothing Then
GetTextBoxValue = o.Object.Text
End If
End Function
Then call the function from a cell, like: =GetTextBoxValue("TextBox1")

Retrieving Cell name in Excel

Is there a way of displaying the cell name of a particular cell in another cell? I would like to display the cell name in the adjacent cell so that the user is able to identify the cell name without clicking it.
ADDRESS(ROW(),COLUMN()) will give you the address, e.g. $A$1 of the current cell. Add/subtract from the row/column values (numbers) to reference the cell you are after.
If you don't want the $ then you can find and replace it with SUBSTITUTE(ADDRESS(ROW(),COLUMN()),"$","") and get just A1 for example
This function would give the name of the NamedRange the cell belongs to:
Public Function CellName(oCell As Range) As Variant
Dim oName As Name
For Each oName In ThisWorkbook.Names
If oName.RefersToRange.Parent Is oCell.Parent Then
If Not Intersect(oCell, oName.RefersToRange) Is Nothing Then
CellName = oName.Name
Exit Function
End If
End If
Next
CellName = CVErr(xlErrNA)
End Function
It loops through all the names in the workbook, and then for each name it checks if it refers to any thing in this the sheet input parameter is from. If it is then it checks if the input cell and the rages referred by the name intersect. If they do it returns the name of the range.
In Excel 2013, and maybe in some older versions too, ADDRESS() accepts third parameter that defines the format of the address to be returned with following values:
1 - Absolute (default)
2 - Absolute row/Relative column
3 - Relative row/Absolute column
4 - Relative
so lets say in cell A1
ADDRESS(ROW();COLUMN()) //outputs $A$1
ADDRESS(ROW();COLUMN();1) //outputs $A$1
ADDRESS(ROW();COLUMN();2) //outputs A$1
ADDRESS(ROW();COLUMN();3) //outputs $A1
ADDRESS(ROW();COLUMN();4) //outputs A1
If you want to display the name of cell D3 in cell A1 type:
ADDERSS(ROW(D3);COLUMN(D3);4) //outputs text D3 in cell A1
Technically, you could combine the SUBSTITUTE() and new FORMULATEXT() functions to do this, if you don't mind adding a hidden column in your sheet.
Assume cell A1 is named FOO
Add a simple reference formula in B1 to the named cell =FOO
Add a formula in C1 =SUBSTITUTE(FORMULATEXT(B2),"=","")
Hide Column B
Cell C1 will contain the value FOO (or the RC reference for unnamed cells)
Adjust as needed, your mileage (or kilometerage) may vary.
It doesn't seem to be possible, which is weird. You'd think that the cell() function should provide a way to get the name, but it doesn't. Bummer.
ActiveWorkbook.Sheets.Item(1).Cells(row, col).Name.Name
I took some 'advice' from the answer above from Adarsha. I got a similar result to the code below, with that loop and a few refinements. However my 'tip for excel' macros is to make your debugger your best friend.
Function name_of(clls)
'
name_of = ""
'
Dim nam As String
Dim rg As Range
Set rg = clls
'
nam = rg.Name.Name
'
name_of = nam
'
End Function 'name_of
A little bit of patience and perseverance, gave me exactly what I was looking for -- A user defined function to give me the defined name of a cell. What happens if the same Cell has more than one name? Try that and see. It is a simple test, so I want to leave that for you to experiment, learn and pass on your new knowledge.
This will work in very basic circumstances:
Public Function CellName(cel As Range) As Variant
Dim nm As Name
For Each nm In Names
If nm.RefersTo = "=" & cel.Parent.Name & "!" & cel.Address Then
CellName = nm.Name
Exit Function
End If
Next
CellName = CVErr(xlErrNA)
End Function
It won't work if the cell is part of a named range, it won't show multiple names for the cell, it won't work for cells included in named formulae (like =OFFSET() ranges, for example).
The
"=" & cel.Parent.Name & "!" & cel.Address
thing is pretty clunky, too. There may be a better way to do the check. Creating a Range object from the RefersTo and using Intersect() might work.
Excel does have a function "Cell()" that you can get certain properties from.
You can use =Cell("row", K9) and get back row number 9 and there's an equivalent "col" parameter but it returns the column number (11) rather than the letter.
Reference the named cell in another cell, E12 in this case, and then use this formula: ="'"&FORMULATEXT(E12). This puts an apostrophe in front so it will show the name or formula as text.
You can place your cursor in an empty cell, type = then click on the named cell. It will display the cell contents. Then you change the format of the cell to text and it will show =

Resources