在X轴上设置日期

时间:2013-11-20 23:05:58

标签: excel vba excel-vba

我正在尝试在Excel VBA中创建一个图表,并且在让X轴正确显示日期方面遇到了问题。代码如下:

Function CreateChart()
Dim objChart As Chart

ReDim detached_price(detachedProps.count - 1) As Double
ReDim detached_date(detachedProps.count - 1) As Date

ReDim semi_price(semiProps.count - 1) As Double
ReDim semi_date(semiProps.count - 1) As Date

Dim minDate As Date
Dim maxDate As Date
minDate = Date

Dim detachedCount As Integer
detachedCount = 0
Dim semiCount As Integer
semiCount = 0

Set objChart = Charts.Add
With objChart
    .HasTitle = True
    .ChartTitle.Characters.Text = "Price Paid"
    .ChartType = xlXYScatter
    .Location xlLocationAsNewSheet
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
    .Axes(xlCategory, xlPrimary).CategoryType = xlTimeScale
    .Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "yyyy"
    .Axes(xlCategory, xlPrimary).MinimumScaleIsAuto = True
    .Axes(xlCategory, xlPrimary).MaximumScaleIsAuto = True

    For Each prop In properties
        Select Case prop.PropertyType
            Case "Detached"
                detached_price(detachedCount) = prop.Amount
                detached_date(detachedCount) = prop.SellDate
                detachedCount = detachedCount + 1
            Case "Semi-detached"
                semi_price(semiCount) = prop.Amount
                semi_date(semiCount) = prop.SellDate
                semiCount = semiCount + 1
        End Select

        If prop.SellDate < minDate Then
            minDate = prop.SellDate
        End If

        If prop.SellDate > maxDate Then
            maxDate = prop.SellDate
        End If
    Next

    .SeriesCollection.NewSeries

    .SeriesCollection(DETACHED).Name = "Detached"
    .SeriesCollection(DETACHED).Values = detached_price
    .SeriesCollection(DETACHED).XValues = detached_date

    .SeriesCollection.NewSeries
    .SeriesCollection(SEMI).Name = "Semi-Detached"
    .SeriesCollection(SEMI).Values = semi_price
    .SeriesCollection(SEMI).XValues = semi_date
End With End Function

填充For..Each循环中的属性变量,并正确填充数组。

但是,虽然显示了Scatter Graph数据点,但轴上的日期都显示为1900.

我尝试添加这些行:

    .Axes(xlCategory, xlPrimary).MinimumScale = CDbl(minDate)
    .Axes(xlCategory, xlPrimary).MaximumScale = CDbl(maxDate)

其中显示了沿轴的正确年份,但现在两个系列的所有数据点都消失了。

我尝试过其他一些事情,但这纯粹是基于反复试验。

数据如下

The data is as follows:

结果图表是:

更正日期,无数据点

Correct dates, no data points

日期不正确,但我们有数据点

Incorrect dates, but we have data points

1 个答案:

答案 0 :(得分:5)

即使我不同意他们对“常见”日期格式的定义:q,我认为@chiliNUT正在寻找一些东西。强制执行日期格式似乎存在一些问题。

如果您将所有Date类型变量更改为DoubleLong,它应该有效。

例如更改

ReDim detached_date(detachedProps.count - 1) As Date

ReDim detached_date(detachedProps.count - 1) As Double

ReDim detached_date(detachedProps.count - 1) As Long

这样,String方法不会将日期转换为XValues。它们存储为日期序列号,轴例程能够将它们成功强制转换为本地日期格式。

您的代码中发生的事情是Date类型被String方法强制转换为XValues,并且轴渲染例程似乎无法将它们强制转换回日期。< / p>

我不认为它实际上与国际设置有关,因为我尝试使用这样的日期:

1/01/2013
1/01/2014
1/01/2015
1/01/2016
1/01/2017
1/01/2018
1/01/2019
1/01/2020
1/01/2021

适用于任何一种系统。

我认为它只是轴渲染例程中的一个错误,它无法正确地将字符串强制转换为日期。

我很想听听比我更了解的人。

另外,我很好奇:

.SeriesCollection.NewSeries

.SeriesCollection(DETACHED).Name = "Detached"
.SeriesCollection(DETACHED).Values = detached_price
.SeriesCollection(DETACHED).XValues = detached_date

如何知道要引用哪个系列? DETACHED是你在其他地方定义的常量吗?

我认为这会更好:

with objChart.SeriesCollection.NewSeries
    .Name = "Detached"
    .Values = detached_price
    .XValues = detached_date
end with
相关问题