How to trigger macro when data table is recalculated with F9?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
I have the following macro, which modifies a chart when the worksheet is recalculated:

Code:
Private Sub Worksheet_Calculate()

   Sheet1.ChartObjects("Chart 1").Chart.Axes(x1Value).CrossesAt = Range("Base1")

End Sub
How do I get this macro to run ONLY when a data table is recalculated with F9 or on save?

Currently it runs when any part of the sheet is recalculated and blocks the undo function, so I would like to limit this to recalculation of the data table which drives the chart.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Why did F9 change? Because someone typed a value into it or because it has a formula in it which changed its calculated value? If the latter, which other cell(s) changed to cause it to do that?

Could you use the Worksheet_Change handler and check at the start whether the cell which triggered the change is the one you're interested in?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
  If Intersect(Target, [COLOR=red][B]Range("F9")[/B][/COLOR]) Is Nothing Then Exit Sub
  
  Sheet1.ChartObjects("Chart 1").Chart.Axes(x1Value).CrossesAt = Range("Base1")
  
End Sub
 
Upvote 0
Why did F9 change? Because someone typed a value into it or because it has a formula in it which changed its calculated value? If the latter, which other cell(s) changed to cause it to do that?

Could you use the Worksheet_Change handler and check at the start whether the cell which triggered the change is the one you're interested in?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
  If Intersect(Target, [COLOR=red][B]Range("F9")[/B][/COLOR]) Is Nothing Then Exit Sub
  
  Sheet1.ChartObjects("Chart 1").Chart.Axes(x1Value).CrossesAt = Range("Base1")
  
End Sub
Sorry if I was unclear, but I meant F9 as in Shift+Fn9 on the keyboard (not F9 as in the cell reference), since I have set Excel calculation option to "automatic except for data tables". So basically I want to trigger the macro only when the data table is recalculated manually with Shift+Fn9...

How do I do this?
 
Last edited:
Upvote 0
I'm thinking...
 
Last edited:
Upvote 0
You could hook the keystrokes and call your own macro instead:
Code:
Sub SetKeys()
    Application.OnKey "+{f9}", "MyCalc"
End Sub
Sub UnSetKeys()
    Application.OnKey "+{f9}"
End Sub
Sub MyCalc()
    Application.Calculate
   Sheet1.ChartObjects("Chart 1").Chart.Axes(x1Value).CrossesAt = Range("Base1")
End Sub
 
Upvote 0
In the end I found a very simple solution - add a macro button to calculate the tables and update the chart together:

Code:
Private Sub Rectangle9_Click()
 
  ActiveSheet.Calculate
  ActiveSheet.ChartObjects("Chart 1").Chart.Axes(xlValue).CrossesAt = Range("Base1")
 
End Sub

Works like a charm, as the undo function works fine with all the other calculations until this macro button is activated! :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,185
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top