如何在每周的星期一开始制作图表的标签?

时间:2017-08-30 13:49:19

标签: excel-vba vba excel

我试图通过excel图表显示每周基于进度的趋势。使用VBA,如何强制xlCategory轴的刻度标签捕捉到我的数据集中的每个星期一?这可能吗?我知道如何格式化轴,但不知道如何告诉它从每个星期一开始。谢谢!

我知道有一个选项可以更改majorunit和minorunit,也许我可以以某种方式使用它们?这就是我到目前为止所做的:

On Error Resume Next
ActiveSheet.ChartObjects.Delete
On Error GoTo 0

Set metricschart = sht.Shapes.AddChart.Chart

With metricschart
    .Parent.Name = "metricschart"
    .HasTitle = True
    .ChartTitle.Text = "Business Requirements Tested Over Time"
    .ChartTitle.Characters.Font.Size = 14
    .ChartType = xlXYScatterSmoothNoMarkers
    .SetSourceData Source:=sht2.Range("A1:DA2")
    .Location where:=xlLocationAsObject, Name:=sht.Name
    .Parent.Height = 325
    .Parent.Width = 600
    .Parent.Top = 70
    .Parent.Left = 350
    .Legend.LegendEntries(1).Delete
    .Axes(xlCategory).MinimumScale = sht2.Range("A1")
    .Axes(xlCategory).MaximumScale = sht2.Range("DA1")
    .Axes(xlCategory).TickLabels.NumberFormat = "m/d"
    .Axes(xlCategory).MajorUnit = vbMonday 'this doesn't work, shows way too many days
End With

上面的代码使轴像这样(每2天): enter image description here

这就是我的数据: enter image description here

1 个答案:

答案 0 :(得分:3)

诀窍是将类别轴设置为时间刻度,确定起始值的星期一,并且每隔七天添加一次刻度。

Sub Test()
Dim cht As Chart
Dim ax As Axis

Set cht = Worksheets(1).ChartObjects(1).Chart
Set ax = cht.Axes(xlCategory)

With ax
    .CategoryType = xlTimeScale 'For dates
    .MajorUnit = 7 'Only 1 tick every 7 days
    .AxisBetweenCategories = False 'Tick exactly on the date, not between the values.
    .CrossesAt = 42975 'the integer value of the date you wish to be the first one. In this case 2017-08-28.
End With

End Sub