VBA Excel在图表上设置水平轴

时间:2016-03-06 01:05:02

标签: excel-vba charts vba excel

我编译了以下宏来在Excel工作表的右侧创建3个图表。

Sub CreateChart()
Dim objChart As ChartObject
Dim myChtRange As Range
Dim myDataRange As Range
With ActiveSheet

Set myChtRange = Range("M10:Q23")
' What range contains data for chart
Set myDataRange = ActiveSheet.ListObjects("Table2").Range
' Cover chart range with chart
Set objChart = .ChartObjects.Add( _
    Left:=myChtRange.Left, Top:=myChtRange.Top, _
    Width:=myChtRange.Width, Height:=myChtRange.Height)
' Put all the right stuff in the chart
With objChart.Chart
    .ChartArea.AutoScaleFont = False
    .ChartType = xlLine
    .ChartStyle = 245
    .SetSourceData Source:=myDataRange
    .HasTitle = True
    .HasLegend = False
    .ChartTitle.Characters.Text = "PO By Year"
    .ChartTitle.Font.Bold = True
    .ChartTitle.Font.Size = 12

    With .Axes(xlCategory, xlPrimary)
       .HasTitle = True
       With .AxisTitle
            .Characters.Text = "Year"
            .Font.Size = 10
            .Font.Bold = True
        End With
    End With
    With .Axes(xlValue, xlPrimary)
        .HasTitle = True
        .DisplayUnit = xlMillions
        .HasDisplayUnitLabel = False
        With .AxisTitle
            .Characters.Text = "Millions"
            .Font.Size = 10
            .Font.Bold = True
        End With
    End With
End With

Set myChtRange = Range("S10:W23")
' What range contains data for chart
Set myDataRange = ActiveSheet.ListObjects("Table17").ListColumns(5).Range
' Cover chart range with chart
Set objChart = .ChartObjects.Add( _
    Left:=myChtRange.Left, Top:=myChtRange.Top, _
    Width:=myChtRange.Width, Height:=myChtRange.Height)
' Put all the right stuff in the chart
With objChart.Chart
    .ChartArea.AutoScaleFont = False
    .ChartType = xlLine
    .ChartStyle = 245
    .SetSourceData Source:=myDataRange
    .HasTitle = True
    .HasLegend = False
    .ChartTitle.Characters.Text = "Invoices By Year"
    .ChartTitle.Font.Bold = True
    .ChartTitle.Font.Size = 12
    With .Axes(xlCategory, xlPrimary)
        .HasTitle = True
        With .AxisTitle
            .Characters.Text = "Years"
            .Font.Size = 10
            .Font.Bold = True
        End With
    End With
    With .Axes(xlValue, xlPrimary)
        .HasTitle = True
        .DisplayUnit = xlMillions
        .HasDisplayUnitLabel = False
        With .AxisTitle
            .Characters.Text = "Millions"
            .Font.Size = 10
            .Font.Bold = True
        End With
    End With
End With

Set myChtRange = Range("Y10:AC23")
' What range contains data for chart
Set myDataRange = ActiveSheet.ListObjects("Table30").ListColumns(5).Range
' Cover chart range with chart
Set objChart = .ChartObjects.Add( _
    Left:=myChtRange.Left, Top:=myChtRange.Top, _
    Width:=myChtRange.Width, Height:=myChtRange.Height)
' Put all the right stuff in the chart
With objChart.Chart
    .ChartArea.AutoScaleFont = False
    .ChartType = xlLine
    .ChartStyle = 245
    .SetSourceData Source:=myDataRange
    .HasTitle = True
    .HasLegend = False
    .ChartTitle.Characters.Text = "Req to PO"
    .ChartTitle.Font.Bold = True
    .ChartTitle.Font.Size = 12
    With .Axes(xlCategory, xlPrimary)
        .HasTitle = True
        With .AxisTitle
            .Characters.Text = "Time"
            .Font.Size = 10
            .Font.Bold = True
        End With
    End With
    With .Axes(xlValue, xlPrimary)
        .HasTitle = True
        .DisplayUnit = xlMillions
        .HasDisplayUnitLabel = False
        With .AxisTitle
            .Characters.Text = "Days"
            .Font.Size = 10
            .Font.Bold = True
        End With
    End With
End With
End With
End Sub

表I对第一个图表的引用如下所示:

Table Example

结果图表看起来像这样: enter image description here

问题是我无法确定如何在水平轴而不是点1,2,3,4上显示年份....

我是否需要像表2所做的那样引用整个表格,或者我是否需要像参考表17那样仅参考第5列?

宏录制器没有显示从图示的图表到水平轴上有年份的图表的步骤。

1 个答案:

答案 0 :(得分:1)

你试过这样的事吗:

ActiveChart.SeriesCollection(1).XValues = "='Sheet1'!$A$1:$A$4"

其中SeriesCollection(1)是系列的名称,当然还使用实际年份的范围而不是Sheet1等。