0

我有一个宏,它Chart从一系列数据中生成一个。见以下数据:

2015/08/01 12:49.002

2015/08/01 00:41.600

2015/08/02 00:27.198

2015/08/03 01:05.600

2015/08/03 01:30.599

2015/08/04 02:29.799

2015/08/05 01:40.199

2015/08/06 01:36.199

2015/08/07 02:16.998

2015/08/07 00:43.401

第一列代表日期,然后第二列是从该日期开始的时间范围。

注意我在工作表中找到的原始数据是已合并的单元格。有关更多信息,请参阅下面的屏幕截图。

在此处输入图像描述

问题是图表仅显示为该日期分配的较大数字。

请参阅下面的代码。

Option Explicit
Public Declare Function GetTickCount Lib "kernel32.dll" () As Long

Sub CreateChart()

    Dim DateRange, TimeRange As Range
    Dim lastRow As Long
    Dim StartRow As Long, columnIndex As Long
    Dim DataWorkSheet As Worksheet
    Dim DataFileFullPath As String, DataFileName As String, SheetName As String
    Dim Index As Long, Index2 As Long
    Dim t As Long
    Dim tt As Long
    Dim Chart1 As Chart


'    'Disable Screen Updating
'    Application.ScreenUpdating = False
'    Application.Calculation = xlCalculationManual


    StartRow = 20
    columnIndex = 3

    'Put Full File Path for your demo/test file here
    DataFileFullPath = "C:\Users\................."

    Index = InStrRev(DataFileFullPath, "\")
    DataFileName = Right(DataFileFullPath, Len(DataFileFullPath) - Index)

    Index2 = InStrRev(DataFileName, ".")
    SheetName = Left(DataFileName, Index2 - 1)

    Set DataWorkSheet = Workbooks(DataFileName).Sheets(SheetName)



    t = GetTickCount

    With DataWorkSheet

        With .UsedRange
            'Getting the last Row
            lastRow = .Rows(.Rows.Count).row - 1
        End With

        'The DataStartRow is set to the ORiginal Time from the T3000
        Set DateRange = .Range(.Cells(StartRow, columnIndex + 1), .Cells(lastRow, columnIndex + 1))
        Set TimeRange = .Range(.Cells(StartRow, columnIndex + 2), .Cells(lastRow, columnIndex + 2))

    End With

    Set Chart1 = Charts.Add

    With Chart1

        .ChartType = xlColumnClustered
        .SeriesCollection.NewSeries

        With .SeriesCollection(1)
            .Values = TimeRange
            .Name = SheetName & " " & "Synch Time"
            .XValues = DateRange
        End With

        .Name = SheetName & " " & "Synch Time Chart"
        .Axes(xlValue).MaximumScale = 0.0104166667 ' 15 mins / 50 / 24
        .Axes(xlValue).MajorUnit = 0.0006944444 ' 1 mins /60 / 24
        .Move After:=Sheets(2)

    End With
    tt = GetTickCount - t
'    'Enable Screen Updating
'    Application.ScreenUpdating = True
'    Application.Calculation = xlCalculationAutomatic
End Sub

我是否需要包含一个元素Chart1以不省略特定日期的第二个数据值?

4

2 回答 2

1

如果要在 X 轴上重复一天,则需要添加:

.Axes(xlCategory).CategoryType = xlCategoryScale
于 2016-01-13T12:52:05.480 回答
0

使用@Rory 的答案,我看到所有行都显示了。

在做了一些搜索后,我看到在Chart默认情况下,只有显示单元格中的数据(即不隐藏)才会在Chart.

然后,我将 aRange(Rows(x), Rows(y)).Hidden = True合并到脚本中,该脚本为我提供了特定时间段的合并单元格。

下图代表最终产品。

在此处输入图像描述

您可以在图表中看到 2015/08/01 的双重条目

右键单击轴时,.Axes(xlCategory).CategoryType = xlCategoryScale基本上将水平轴类型设置为 Excel 弹出菜单中提到的“文本轴”

欢迎任何其他关于使用 Rory 在他的回答中提到的不同类型图表的建议。

暂时不回答这个问题。

于 2016-01-13T14:08:27.917 回答