ActiveChart.SeriesCollection(1).XValues不显示日期

时间:2017-02-01 06:04:02

标签: excel-vba graph vba excel

我有一个带有36个散点图的工作表“Tracker”。同一工作表包含12组数据,每组包含3个变量,需要将这些变量绘制成相关的日期/时间。我以这样的方式命名它们,即创建循环可以使代码更清晰。

问题在于:这很有效,但所有图表的水平轴都不代表实际值。 ={"1/10/2017 12:11:03 PM","1/10/2017 12:11:06 PM","1/10/2017 12:11:09 PM","1/10/2017 12:11:12 PM","1/10/2017 12:11:15 PM","1/10/2017 12:11:19 PM","1/10/2017 12:11:22 PM","1/10/2017 12:11:25 PM","1/10/2017 12:11:28 PM","1/10/2017 12:11:31 PM"}

以上是XValues系列在您选择数据时所显示的内容,但图表显示的是1/0 / 00,1 / 1 / 100,1 / 2/00,以及X值...我确定了日期列的格式为日期/时间格式。 这是代码:

    For i = 1 To 12
    k = (i * 4) + 49
    RowCount = ThisWorkbook.Sheets("Tracker").Cells(1, k).Offset(Sheets("Tracker").Rows.Count - 1, 0).End(xlUp).Row
    ActiveSheet.ChartObjects("Location" & i & "_1").Activate
    ActiveChart.SeriesCollection(1).Name = Worksheets("Tracker").Cells(1, k + 2).Value
    **ActiveChart.SeriesCollection(1).XValues = Worksheets("Tracker").Range(Cells(2, k), Cells(RowCount, k)).Value**
    ActiveChart.SeriesCollection(1).Values = Worksheets("Tracker").Range(Cells(2, k + 2), Cells(RowCount, k + 2)).Value
    ActiveChart.SeriesCollection(2).Name = Worksheets("Tracker").Cells(1, k + 3).Value
    **ActiveChart.SeriesCollection(2).XValues = Worksheets("Tracker").Range(Cells(2, k), Cells(RowCount, k)).Value**
    ActiveChart.SeriesCollection(2).Values = Worksheets("Tracker").Range(Cells(2, k + 3), Cells(RowCount, k + 3)).Value

    ActiveSheet.ChartObjects("Location" & i & "_2").Activate
    ActiveChart.SeriesCollection(1).Name = Worksheets("Tracker").Cells(1, k + 1).Value
    **ActiveChart.SeriesCollection(1).XValues = Worksheets("Tracker").Range(Cells(2, k), Cells(RowCount, k)).Value**
    ActiveChart.SeriesCollection(1).Values = Worksheets("Tracker").Range(Cells(2, k + 1), Cells(RowCount, k + 2)).Value

    l = (i * 2) + 102
    RowCount1 = ThisWorkbook.Sheets("Tracker").Cells(1, l).Offset(Sheets("Tracker").Rows.Count - 1, 0).End(xlUp).Row
    ActiveSheet.ChartObjects("Location" & i & "_3").Activate
    ActiveChart.SeriesCollection(1).Name = Worksheets("Tracker").Cells(1, l + 1).Value
    **ActiveChart.SeriesCollection(1).XValues = Worksheets("Tracker").Range(Cells(2, l), Cells(RowCount1, l)).Value**
    ActiveChart.SeriesCollection(1).Values = Worksheets("Tracker").Range(Cells(2, l + 1), Cells(RowCount1, l + 2)).Value

    Next i

当我将鼠标悬停在图表上的数据点上时,日期显示在文本中,但似乎它正在绘制Y值,数字从1开始,而不是完全按日期/时间。

在过去,我刚刚用关联的系列写出每个图表,但由于有36个图表,我不想为每个图表做这个 - 这很疯狂。 使用这个我没有问题:

    ActiveChart.SeriesCollection(1).Name = "=" & "Location" & i & "!$AC$1"
    ActiveChart.SeriesCollection(1).XValues = "=" & "Location" & i & "!$AA$1:$AA$" & RowCount1
    ActiveChart.SeriesCollection(1).Values = "=" & "Location" & i & "!$AC$1:$AC$" & RowCount1
    ActiveChart.SeriesCollection(2).Name = "=" & "Location" & i & "!$AD$1"
    ActiveChart.SeriesCollection(2).XValues = "=" & "Location" & i & "!$AA$1:$AA$" & RowCount1
    ActiveChart.SeriesCollection(2).Values = "=" & "Location" & i & "!$AD$1:$AD$" & RowCount1

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

首先,您应该尝试将ChartObject对象设置为Activate,并且无需使用SeriesCollection来修改图表。

您可以使用ChartObjectFor j= 1 To .Chart.SeriesCollection.Count中循环所有Option Explicit Sub AutoChartTest() Dim ChtObj As ChartObject Dim Ser As SeriesCollection Dim SerRng As Range Dim j As Long, i As Long, RowCount1 As Long ' set the chart object Set ChtObj = ActiveSheet.ChartObjects("Location" & i & "_1") With ChtObj '<-- modify properties of the chart object (without Activating it) For j = 1 To .Chart.SeriesCollection.Count ' <-- loop through all series collections Set SerRng = Range("AC1").Offset(, j - 1) .Chart.SeriesCollection(j).Name = "=" & "Location" & i & "!" & SerRng.Address .Chart.SeriesCollection(j).XValues = "=" & "Location" & i & "!$AA$1:$AA$" & RowCount1 .Chart.SeriesCollection(j).Values = "=" & "Location" & i & "!" & SerRng.Address & ":$AC$" & RowCount1 Next j End With End Sub

<强>代码

<config evaluator="string-compare" condition="DocLibActions">
                <actions>
                    <action id="transform-to-pdf" type="javascript" label="Download as PDF">
                        <param name="function">convertAndDownload</param>
                        <evaluator negate="true">alfresco.tutorials.evaluator.checkFileType
                        </evaluator>
                    </action>
                </actions>

                <actionGroups>
                    <actionGroup id="document-browse">
                        <action index="400" id="transform-to-pdf" />
                    </actionGroup>
                </actionGroups>
            </config>

            <config evaluator="string-compare" condition="DocLibCustom"
                replace="true">
                <dependencies>
                    <js src="components/myactions/actions.js" />
                </dependencies>
            </config>