散点图vba中的日期和数值

时间:2016-04-15 03:45:29

标签: excel vba excel-vba plot

要存储日期而不考虑系统格式,我将其设置为字符串,因为excel将被不同系统上的许多用户使用。以下是代码:

    Range("Z:Z").NumberFormat = "@"
    Range("AC:AC").NumberFormat = "@"
    Range("AF:AF").NumberFormat = "@"
    dateArr = Array("4/1/2016", "4/15/2016", "5/1/2016", "5/15/2016", "6/1/2016", "6/15/2016", "7/1/2016", "7/15/2016", "8/1/2016", "8/15/2016", "9/1/2016", "9/15/2016", "10/1/2016", "10/15/2016", "11/1/2016", "11/15/2016", "12/1/2016", "12/15/2016")
    For i = 2 To UBound(dateArr)
        ActiveSheet.Cells(i, 26).Value = Format(dateArr(i - 2), "yyyy/mm/dd")
    Next

但我需要为它绘制散点图,所以我再次将其更改为日期格式,如下所示:

Columns("Z:Z").Select
Selection.NumberFormat = "yyyy/mm/dd"

当我绘制散点图时,它没有正确地绘制它。

  ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLines
ActiveChart.SetSourceData Source:=Range("Plot!$Z$2:$AA$" & date_no_row)
ActiveChart.Axes (xlCategory)
With ActiveChart.Axes(xlCategory)
    .MinimumScale = 42401
    .TickLabels.Orientation = 30
    .TickLabels.NumberFormat = "yyyy/mm/dd"
    .BaseUnitIsAuto = True
    .MajorUnit = 14
    .MinorUnitIsAuto = True
    .Crosses = xlAutomatic
    .ReversePlotOrder = False
End With
ActiveChart.Parent.Name = "Plot"
With ActiveChart.Parent
     .Height = 325 ' resize
     .Width = 900  ' resize
     .Top = Range("C3").Top
     .Left = Range("B2").Left
End With
ActiveSheet.ChartObjects("Plot").Chart.HasLegend = False
With ActiveSheet.ChartObjects("Plot").Chart
    .HasTitle = True
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Dates"
    .Axes(xlCategory, xlPrimary).AxisTitle.Font.Size = 15
End With

ActiveSheet.ChartObjects("Plot").Activate
ActiveChart.Axes(xlCategory).HasMajorGridlines = True

列AA包含数值。我得到的情节如下图所示

enter image description here

预期情节是

enter image description here

1 个答案:

答案 0 :(得分:1)

您将列Z设置为文本。之后,您尝试设置数字格式,但单元格是文本,因此数字格式永远不会。试试吧。应用前两个代码段,然后手动更改单元格格式。它没有任何效果。

因此,从列Z的正确格式开始,

Range("Z:Z").NumberFormat = "yyyy/mm/dd"