首页 > 解决方案 > Trying to add 'year' to X axis in an Excel file

问题描述

I have the following chart, and I want to show each year under its specific data: 2000, 2001, 2002, ... 2016, instead of the general (Year 2000-2016) caption which can be seen right now.

enter image description here

But when I try to add years, the whole chart becomes unstable and my information gets lost! How should I do this?

标签: excelcharts

解决方案


我的假设是您在创建图表时只选择了 B 列。您应该尝试创建一个新图表,但这次选择两列。在列中添加标题也很好(我假设图表中的标题是手写的)。我想为了显示第二个轴值,您应该单击图表右上角的“+”按钮。应出现一个菜单(图表元素)。在此菜单中,您将看到轴条目。将光标移到它上面,它的一侧应该会出现一个黑色三角形。点击那个三角形。应出现一个子菜单。在其中您将看到 3 个条目:主要水平、主要垂直和其他选项。主要垂直已经处于活动状态,主要水平不是。单击它以将其打开。现在您的水平轴将具有指定的值。它们可能是从 1 到 17 的数字。那是因为您尚未创建同时选择 A 列和 B 列的图表。这就是我建议您创建新图表的原因。您也可以编辑您的实际图表以覆盖 A 列,但我不知道您还想阅读我的解释多久。:)

无论如何,这段代码也应该给你你正在寻找的图表:

Sub SubAddChart()

    'Declarations.
    Dim Report As Object

    'Creating data.
    Range("A1").FormulaR1C1 = "Number of uses in Neurosurgery"
    Range("A3").FormulaR1C1 = "2000"
    Range("A4:A19").FormulaR1C1 = "=R[-1]C+1"
    Range("A2").FormulaR1C1 = "=""YEAR ("" & MIN(" & Range("A3:A19").Address(True, True, xlR1C1) & ") & ""-"" & MAX(" & Range("A3:A19").Address(True, True, xlR1C1) & ") & "")"""
    Range("B2").FormulaR1C1 = "Number of uses"
    Range("B3").FormulaR1C1 = "0"
    Range("B4").FormulaR1C1 = "0"
    Range("B5").FormulaR1C1 = "2"
    Range("B6").FormulaR1C1 = "2"
    Range("B7").FormulaR1C1 = "1"
    Range("B8").FormulaR1C1 = "1"
    Range("B9").FormulaR1C1 = "8"
    Range("B10").FormulaR1C1 = "14"
    Range("B11").FormulaR1C1 = "15"
    Range("B12").FormulaR1C1 = "12"
    Range("B13").FormulaR1C1 = "13"
    Range("B14").FormulaR1C1 = "17"
    Range("B15").FormulaR1C1 = "21"
    Range("B16").FormulaR1C1 = "25"
    Range("B17").FormulaR1C1 = "31"
    Range("B18").FormulaR1C1 = "24"
    Range("B19").FormulaR1C1 = "29"

    'Creating chart.
    Set Report = ActiveSheet.Shapes.AddChart2(201, xlColumnClustered)

    With Report
        With .Chart
            'Setting source data.
            .SetSourceData Source:=Range(Range("A2"), Range("A2").End(xlDown).Offset(0, 1)), PlotBy:=xlColumns
            'Deleting years series collection.
            .SeriesCollection(Range("A2").Value).Delete
            'Changing X axis labels.
            .FullSeriesCollection(Range("B2").Value).XValues = "=" & ActiveSheet.Name & "!" & Range(Range("A3"), Range("A2").End(xlDown)).Address(, , xlR1C1)
            'Adding chart title.
            .HasTitle = True
            .ChartTitle.Caption = "=" & ActiveSheet.Name & "!" & Range("A1").Address(, , xlR1C1)
            'Adding axis title.
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Caption = "=" & ActiveSheet.Name & "!" & Range("B2").Address(, , xlR1C1)
            .Axes(xlCategory, xlPrimary).AxisTitle.Caption = "=" & ActiveSheet.Name & "!" & Range("A2").Address(, , xlR1C1)
            'Adding data labels.
            .SetElement (msoElementDataLabelOutSideEnd)
        End With
    End With

End Sub

要使其工作,您可以右键单击要放置图表的工作表标签(我建议第一次尝试使用新工作表),单击“显示代码”,粘贴代码,单击它,按 F5。

额外说明:我会指向一个像这样的图表介绍教程:https ://www.youtube.com/watch?v=DAU0qqh_I-A. 跟随它应该能够获得他正在寻找的图表。这个问题背后的主要问题是,在创建图表时,并非所有数据都被选择/指定。仅选择 B 列中的数据会为 Excel 提供部分数据。不够制作图表,而不是制作我们想要的图表。Excel 通常不会在选定/指定范围之外查找标签或额外数据。我们首先需要指出它们。选定/指定的范围是 Excel 在创建图表时将考虑的全部范围。因此,通过仅选择 B 列,Excel 将不知道 A 列中的数据应该是我们图表中的数据标签。图表标题和轴标题是手动添加的。如果我们要在数据顶部添加一些标题,这可以由 Excel 本身自动完成;当然,我们还应该在创建图表时选择/指定这些标题。还必须考虑是否将所需元素(如数据标签)设置在图表中。要做到这一点,可以使用图表附近的右上角加号按钮(一旦选择)来设置图表中的元素。


推荐阅读