0
votes

I have this Gantt Chart and i'm looking at Planned Vs Actual dates. the problem is that the chart wont sync the start/end date.

i'm trying to link the max/min values to the cell with VBA but it wont to it.

I tried to use this site: https://peltiertech.com/link-excel-chart-axis-scale-to-values-in-cells/

but his code doesn't work for my chart.

here is a picture: enter image description here

the dates MUST be the same, and change together if i change some values in the table on the right any ideas? thanks

Private Sub Worksheet_Change(ByVal Target As Range)
With ActiveSheet.ChartObjects("Chart 2").Chart 
Select Case Target.Address 

Case "$G$161" 
.Axes(xlCategory).MaximumScale = Target.Value 

Case "$F$163" 
.Axes(xlCategory).MinimumScale = Target.Value 

Case "$G$161" 
.Axes(xlValue).MaximumScale = Target.Value 

Case "$F$163" 
.Axes(xlValue).MinimumScale = Target.Value 

End Select 
End With 
End Sub
2
Please share us the code that you have tried. Thanksnishit dey
Private Sub Worksheet_Change(ByVal Target As Range) With ActiveSheet.ChartObjects("Chart 2").Chart Select Case Target.Address Case "$G$161" .Axes(xlCategory).MaximumScale = Target.Value Case "$F$163" .Axes(xlCategory).MinimumScale = Target.Value Case "$G$161" .Axes(xlValue).MaximumScale = Target.Value Case "$F$163" .Axes(xlValue).MinimumScale = Target.Value End Select End With End Subaj_bk
just like from the siteaj_bk
It is possible In scatter chart to set Axes(xlCategory). And it is applied to Axes only.Dy.Lee
Well, im not using a scatter chart. is there a macro for a bar chart?aj_bk

2 Answers

1
votes

The code you have re-used is a simple demonstration program which allows the axes of a chart to be manipulated when one of 6 specific cells on the worksheet is changed. However, these changes are intended to be made by the user through the keyboard. Making a change in this way triggers the Worksheet_Change event and allows the changed cell to be identified. The code within the event routine modifies the chart's axes according to which specific worksheet cell has been changed.

Your issue, as David Ziemens noted in his comment, is that if a cell changes value because it is recalculated through a formula then this change does not trigger a Worksheet_Change event. (Of course, it is entirely possible that a manually entered change elsewhere on the worksheet will have triggered both a Worksheet_Change event and a recalculation causing a cell of interest to change its value via a formula. In this case, however, the Target argument will identify the cell that was manually changed rather than the one recalculated through a formula.)

Throw away the Select ... Case structure, that is really only useful in the context of the demonstration program you are trying to re-use. Instead, change the code so that it unconditionally updates all 4 of your chart properties directly from cells G161 and F163 (so use ws.Range("G161").Value and ws.Range("F163").Value instead of Target.Value for assigning the chart properties, where ws represents whichever worksheet these cells are located on - eg ActiveSheet or Worksheets("Sheet1") or whatever the worksheet is called). Wrap the assignment of the chart properties inside a Workbook_SheetChange sub rather than Worksheet_Change and your chart will update whenever the workbook recalculates.

This is not a very pure solution in that it does not detect whether cells F163 and G161 actually change when recalculation takes place. So it runs the risk that the 4 properties are being unnecessarily assigned with values that are unchanged. However, unless you have a very large workbook and you are pushing the limits of what your computer can handle, this won't matter from a practical point of view.

0
votes
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
With ActiveSheet.ChartObjects("Chart 2").Chart
ActiveSheet.ChartObjects("Chart 2").Activate
' for main axes
ActiveChart.Axes(xlValue).MaximumScale = Worksheets("Single 
Tool").Range("$G$161").Value
ActiveChart.Axes(xlValue).MinimumScale = Worksheets("Single 
Tool").Range("$F$163").Value

'for secondary axes
ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = Worksheets("Single 
Tool").Range("$G$161").Value
ActiveChart.Axes(xlValue, xlSecondary).MinimumScale = Worksheets("Single 
Tool").Range("$F$163").Value


End With
End Sub

i found that this works best. thank you all