Excel named range with relative reference is not calculating - excel

I have a named range called "cellabove" in my workbook that always references the cell above the cell it is used in. The RefersToR1C1 property of the name is "=!R[-1]C". This allows it to be applied on any worksheet in the workbook.
When I run a macro that deletes some rows then some cells containing cellabove in their formulas are not recalculating, even though I end the macro with application.calculatefullrebuild. However when I then manually force a full rebuild by pressing CTRL+SHIFT+ALT+F9 the cells do recalculate.
Any idea of how I can force the relative referenced named range to refresh?

Using refersto that start with ! in Defined Name formulas is IMHO not advisable as there are long-standing bugs.
You could use =INDIRECT("R[-1]C",FALSE) instead

Try adding one of these calculation options into your code to see if they have an effect.
Sub Calculate_It()
ActiveSheet.Calculate
''// Equivalent to Shift+F9 Recalculates the active worksheet
Application.Calculate
''// Equivalent to F9 Recalculates all worksheets in all open workbooks
Application.CalculateFull
''// Equivalent to Ctrl+Alt+F9 Recalculates all worksheets in all open workbooks (Full recalculation)
Application.CalculateFullRebuild
''// Equivalent to Shift+Ctrl+Alt+F9 Rebuilds the dependency tree and does a full recalculation
End Sub

Related

Read One Cell and Copy to Another Only Upon Opening Spreadsheet

I need to copy the data in cell c3 to cell i11 when I open my spreadsheet. I do not want i11 to change after this action until I reopen the spreadsheet.
You can set the code in the ThisWorkbook part of your projects. Select Workbook and Open or use the following code.
The working formula is the value you see me do below, but this can be achieved a number of way such as using Cells(3,3).Value. Additionally the way the sheet is referenced can vary. I put both the Activesheet reference and a explicit sheet reference, depending how your sheet is structured you may have to use one over the other, but choose one below and see how your sheet handles it and if adjustments are needed.
Private Sub Workbook_Open()
'Use one of these depending on how your sheet opens
ActiveSheet.Range("I11").Value = ActiveSheet.Range("C3").Value
Worksheet("YourWorksheetHere").Range("I11").Value = Worksheet("YourWorksheetHere").Range("C3").Value
End Sub

Use the 'offset' method to reference a cell in another worksheet

I'm working on a routine that will populate a worksheet from data on a second worksheet in the same active workbook. The location on the destination worksheet is relative to a given cell which is the active cell on the relevant worksheet. In order to avoid continually swapping between active sheets, I was hoping that I could reference the destination cell using the 'offset' method, however I can't get it to work. My code line would be something like this:
Worksheets("DestinationSheet").activecell.offset(Rowoffset:=x, ColumnOffset:=y).Value=DataValue
Where x, y, and Datavalue are variables.
How about
Worksheets("DestinationSheet").range(activecell.address).offset(Rowoffset:=x, ColumnOffset:=y).Value=DataValue
?
The activecell is only a single cell on the active sheet so cannot be located on another sheet (and that sheet must be active when the macro is run). Btw it's not a good idea to base code on the activecell if you can avoid it.
That said, I'm not sure I understand what you are doing.

Can I prevent cells being dirtied when a Workbook opens [Mac]?

I have a UDF that I'm using throughout a Workbook, when I save and reopen the Workbook and then try to recalculate a single cell, Excel will update that cell but it will also recalculate every other cell containing that formula in the Workbook.
Here's a simple formula that demonstrates this issue:
Public Function MyFormula(a As Integer, b As Integer) As Integer
Debug.Print "calculating: " & Application.Caller.Address
MyFormula = a * b
End Function
I would expect that after recalculating a single cell to see a single log statement, but I'm actually seeing 4 (I have 4 cells with this UDF in the Workbook).
I'm guessing that when I save & reopen the Workbook, Excel is marking the cells as "Dirty" and in need of recalculation, but that doesn't make sense. According to the docs (https://learn.microsoft.com/en-us/office/client-developer/excel/excel-recalculation#dependence-dirty-cells-and-recalculated-cells) dirty cells are defined as those who's dependancies have changed:
When new data or new formulas are entered, Excel marks all the cells that depend on that new data as needing recalculation. Cells that are marked in this way are known as dirty.
My UDF isn't volatile and I haven't altered any dependancies, so why is Excel recalculating the all cells and how can I stop it from doing so?
(By the way, I'm aware that Manual calculation mode exists. I'd like to be able to use Automatic mode and allow automatic cell updates, but just only when dependancies have changed)
I've since found that this issue is only present on Excel for Mac (tested on the 2016 version).

Force Excel Formula to Reference Current Workbook

I have a simple formula reference that I use in my workbook, however it gets complicated when I use another function that instantly opens my default worksheet and copies it over to my active workbook.
The problem is that the cells in this workbook reference another sheet in my default workbook. The sheet in that and all the other workbooks I am working on has the same name. It's "Form"
When I use my code to copy the sheet over, the cell automatically changes it's reference to include the previous workbook.
I want the formula to ALWAYS USE THE CURRENT WORKBOOK.
Here is what I use
=Form!B6
Here is what I end up getting when i drop the sheet
="filepath"Form!B6
Here is a way to copy a formula from one workbook to another with no changes:
Sub ytrewq()
Dim s As String
s = Workbooks("book2.xlsm").Sheets("Sheet1").Range("G8").Formula
Workbooks("temp.xlsm").Sheets("Sheet1").Range("H1").Formula = s
End Sub
The trick is to use INDIRECT(). For example:
=INDIRECT("Form!B6")

Automatically update a cell in Excel with the current date anytime any cell in a column is updated or changed

Not sure if this is possible in Excel, but I would like cell K9 to be updated with today's date every time ANY cell in K11:K119 is changed. Some cells in K11:K119 are blank, but can have data entered into them at any time. Cells in K11:K119 that already have data could change. So I would like cell K9 to update with today's date any time any single blank cell in K11:K119 is updated with data, or any time any single cell in K11:K119 with data is changed. Hope that makes sense.
Place the following Event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r1 As Range, r2 As Range
Set r1 = Range("K9")
Set r2 = Range("K11:K119")
If Not Intersect(Target, r2) Is Nothing Then
Application.EnableEvents = False
r1.Value = Date
Application.EnableEvents = True
End If
End Sub
The macro monitors your changes to the worksheet. If you change any value in the input range, the cell with the date is refreshed.
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
If you don't want to enable macros, try something simpler:
=IF(COUNT(K11:K119)>=0,TODAY(),0)
COUNT(range) will always be ≥0 (so it doesn't matter what you put as the third argument) and it always updates when any member of the range is changed.

Resources