[英]Setting X-Axis Scale in Chart
I've written a program to set the range of values visible in an excel chart based on user input. 我编写了一个程序,用于根据用户输入设置在Excel图表中可见的值范围。 The program works when there are dates in the x-axis. 当x轴中有日期时,该程序将运行。 It throws an error when I try to modify the code for an x-axis that has a number rather than a date. 当我尝试修改具有数字而不是日期的x轴的代码时,它将引发错误。
This is the code that works (x-axis of chart is an excel formatted date): 这是有效的代码(图表的x轴是excel格式的日期):
Sub xaxis_reset()
Dim start_date As Variant
Dim end_date As Variant
Dim w As Long, ws As Long
Dim z As Long, obj As Long
start_date = InputBox("Start Date")
end_date = InputBox("End Date")
ws = ActiveWorkbook.Worksheets.Count - 2
For w = 1 To ws
obj = Worksheets(w).ChartObjects.Count
For z = 1 To obj
Worksheets(w).ChartObjects(z).Activate
With ActiveChart
.Axes(xlCategory).MinimumScale = DateValue(start_date)
.Axes(xlCategory).MaximumScale = DateValue(end_date)
End With
Next z
Next w
End Sub
This is my attempt to modify the code to work for a chart that has numbers (year-months) in the x-axis. 这是我尝试修改代码以使其在x轴上具有数字(年-月)的图表中起作用的尝试。
Sub xaxis_reset()
Dim start_yr_mnth_cd As Variant
Dim end_yr_mnth_cd As Variant
Dim w As Long, ws As Long
Dim z As Long, obj As Long
start_yr_mnth_cd = InputBox("Start YR_MNTH_CD")
end_yr_mnth_cd = InputBox("End YR_MNTH_CD")
ws = ActiveWorkbook.Worksheets.Count - 2
For w = 1 To ws
obj = Worksheets(w).ChartObjects.Count
For z = 1 To obj
Worksheets(w).ChartObjects(z).Activate
With ActiveChart
.Axes(xlCategory).MinimumScale = start_yr_mnth_cd
.Axes(xlCategory).MaximumScale = end_yr_mnth_cd
End With
Next z
Next w
End Sub
The line .Axes(xlCategory).MinimumScale = start_yr_mnth_cd
throws an error. 行.Axes(xlCategory).MinimumScale = start_yr_mnth_cd
引发错误。
I've tried changing start_yr_mnth_cd
and end_yr_mnth_cd
from Variant
to Long
. 我尝试将start_yr_mnth_cd
和end_yr_mnth_cd
从Variant
更改为Long
。 I've also tried changing the x-axis values in the chart to the general format instead of number. 我还尝试将图表中的x轴值更改为常规格式,而不是数字。 I've made similar changes to the source data in the excel file, even though I don't think that should make a difference. 我对excel文件中的源数据进行了类似的更改,尽管我认为这不会有所作为。 I'm not sure where else to look at this point. 我不确定在这一点上还有什么地方。
Just in case it helps, this is what the x-axis source data looks like: 以防万一,这就是x轴源数据的样子:
201701
201702
201703
201704
201705
201706
201707
201708
201709
201710
201711
201712
201801
201802
201803
201804
201805
201806
201807
Try using the code below (Copy Everything). 尝试使用下面的代码(复制所有内容)。 The private function I added basically takes a YYYYMM format and converts it to a date format that excel understand. 我添加的私有函数基本上采用YYYYMM格式,并将其转换为Excel可以理解的日期格式。
Sub xaxis_reset()
Dim start_yr_mnth_cd As Variant
Dim end_yr_mnth_cd As Variant
Dim w As Long, ws As Long
Dim z As Long, obj As Long
start_yr_mnth_cd = InputBox("Start YR_MNTH_CD")
end_yr_mnth_cd = InputBox("End YR_MNTH_CD")
start_yr_mnth_cd=ConvertYYYYMM_To_Date(start_yr_mnth_cd)
end_yr_mnth_cd=ConvertYYYYMM_To_Date(end_yr_mnth_cd)
ws = ActiveWorkbook.Worksheets.Count - 2
For w = 1 To ws
obj = Worksheets(w).ChartObjects.Count
For z = 1 To obj
Worksheets(w).ChartObjects(z).Activate
With ActiveChart
.Axes(xlCategory).MinimumScale = start_yr_mnth_cd
.Axes(xlCategory).MaximumScale = end_yr_mnth_cd
End With
Next z
Next w
End Sub
Private Function ConvertYYYYMM_To_Date(vDate As Variant) As Date
ConvertYYYYMM_To_Date = DateValue(Right(vDate, 2) & "-" & Left(vDate, 4))
End Function
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.