Reputation: 1077
I've written a program to set the range of values visible in an excel chart based on user input. The program works when there are dates in the x-axis. It throws an error when I try to modify the code for an x-axis that has a number rather than a date.
This is the code that works (x-axis of chart is an excel formatted date):
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.
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.
I've tried changing start_yr_mnth_cd
and end_yr_mnth_cd
from Variant
to Long
. I've also tried changing the x-axis values in the chart to the general format instead of number. I've made similar changes to the source data in the excel file, even though I don't think that should make a difference. 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:
201701
201702
201703
201704
201705
201706
201707
201708
201709
201710
201711
201712
201801
201802
201803
201804
201805
201806
201807
Upvotes: 0
Views: 215
Reputation: 157
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.
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
Upvotes: 1