我有一个代码集可根据单元格值自行更新x轴。我希望这些值显示为日期,但使用标准文本类型的格式(例如,我有样式月 - 年,1月 - 16,作为我的x轴上的每个点)。但是,因为用户在代码中引用的这些单元格中键入了他们想要的最小值和最大值,所以我希望它只能被选为文本,以便此代码可以识别并绘制它。我在下面标有**的行上收到错误。我不知道该怎么做。谢谢!
With ActiveSheet.ChartObjects("Chart 2").chart
Select Case Target.Address
Case "$L$37"
**.Axes(xlCategory).MaximumScale = Target.Value**
Case "$L$38"
.Axes(xlCategory).MinimumScale = Target.Value
Case "$L$39"
.Axes(xlCategory).MajorUnit = Target.Value
Case "$O$37"
.Axes(xlValue).MaximumScale = Target.Value
Case "$O$38"
.Axes(xlValue).MinimumScale = Target.Value
Case "$O$39"
.Axes(xlValue).MajorUnit = Target.Value
End Select
End With
答案 0 :(得分:1)
修改后的代码,currenty我只是手动设置所有单元格地址,只是为了确保图表正在更新,而且确实如此。
Public Sub Update_Chart()
With ActiveSheet.ChartObjects("Chart 2").Chart
.Axes(xlCategory).MaximumScale = Range("$L$37")
.Axes(xlCategory).MinimumScale = Range("$L$38")
.Axes(xlCategory).MajorUnit = Range("$L$39")
' this line you can modifty to whatever format you want your axis to have, the user doesn't need to keep the format on the worksheet cell itself
.Axes(xlCategory).TickLabels.NumberFormat = "mmm-yy" ' "[$-409]d-mmm-yy;@"
.Axes(xlValue).MaximumScale = Range("$O$37")
.Axes(xlValue).MinimumScale = Range("$O$38")
.Axes(xlValue).MajorUnit = Range("$O$39")
' change to whatever format you want your Y-Axis you want
.Axes(xlValue).TickLabels.NumberFormat = "mmm-yy"
End With
End Sub