vba中的date和数值的散点图

为了存储date而不考虑系统格式,我把它作为string,因为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 

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

 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包含数字值。 我得到的情节如下图所示

在这里输入图像说明

和预期的情节是

在这里输入图像说明

您将列Z设置为文本。 之后,您尝试设置一个数字格式,但单元格是文本,所以数字格式永远不会。 尝试一下。 应用前两个代码片段,然后手动更改单元格格式。 这将不起作用。

所以,从列Z的正确格式开始,

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