简体   繁体   English

将Excel图表轴比例尺链接到单元格中的值

[英]Link Excel Chart Axis Scale to Values in Cells

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. 我正在尝试将最大/最小值链接到带有VBA的单元格,但它不会。

I tried to use this site: https://peltiertech.com/link-excel-chart-axis-scale-to-values-in-cells/ 我尝试使用此网站: 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: 这是一张图片: 在此处输入图片说明

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

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. 您重复使用的代码是一个简单的演示程序,当更改工作表上的6个特定单元格之一时,可以操纵图表的轴。 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. 以这种方式进行更改将触发Worksheet_Change事件,并允许标识已更改的单元格。 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. 正如David Ziemens在其评论中指出的那样,您的问题是,如果某个单元格因为通过公式重新计算而更改了值,则此更改不会触发Worksheet_Change事件。 (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.) (当然,在工作表上其他位置手动输入的更改完全有可能触发Worksheet_Change事件和重新计算,从而引起感兴趣的单元格通过公式更改其值。但是,在这种情况下, Target参数将确定手动更改的单元格,而不是通过公式重新计算的单元格。)

Throw away the Select ... Case structure, that is really only useful in the context of the demonstration program you are trying to re-use. 放弃“ Select ... Case结构,该结构仅在您要重用的演示程序的上下文中才真正有用。 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). 而是更改代码,以使其无条件地直接从单元格G161F163更新所有四个图表属性(因此,请使用ws.Range("G161").Valuews.Range("F163").Value而不是Target.Value用于分配图表属性,其中ws表示无论哪个工作表,这些细胞位于上-例如ActiveSheetWorksheets("Sheet1")或任何工作表被调用)。 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. 将工作表属性的分配包装在Workbook_SheetChange子项(而不是Worksheet_Change并且只要工作簿重新计算,您的图表就会更新。

This is not a very pure solution in that it does not detect whether cells F163 and G161 actually change when recalculation takes place. 这不是一个非常纯净的解决方案,因为它无法检测到重新计算时单元格F163G161是否实际上发生了变化。 So it runs the risk that the 4 properties are being unnecessarily assigned with values that are unchanged. 因此,冒着不必要地为4个属性分配不变的值的风险。 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. 但是,除非您的工作簿非常大,并且您要突破计算机可以处理的限制,否则从实际的角度来看这无关紧要。

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 谢谢你们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM