【问题标题】:Chart object formatting errors图表对象格式错误
【发布时间】:2015-09-27 02:59:38
【问题描述】:

我不知道如何正确格式化日期(Xaxis),否则,下面的代码可以工作。 MyArY()contains 日期,但由于某种原因,日期在 00 年 9 月 1 日图表上显示为 00 年?我尝试了在网上找到的示例,但它们对我不起作用。
以下是工作表上日期的样子: 7/21/15 8:00:00 AM 8/25/15 9/1/15 12:00:00 AM 9/10/15 7/21/15 8/25/15 该图表无法识别日期,它们看起来像这样:

感谢您的帮助。

Sub Build_Chart()
'builds a chart on active sheet

Set objChart = ActiveSheet.ChartObjects.Add _
(Left:=30, Width:=775, Top:=15, Height:=345)
objChart.Chart.ChartType = xlXYScatterLines

End Sub

Sub Add_ChartSeries()

Dim i As Long, l As Long
Dim yAddress_ListItem As String, yAddress_ValuesRange As String
Dim xAddress_ValuesRange As String, xAddress_ListItem As String
Dim cht As Chart
Dim rng As Range, aCell As Range
Dim MyArY() As Variant, MyArX() As Variant
Dim LastRow As Long, iVal As Long
'Dim chSeries As Series

Dim objChartSeriesColl As SeriesCollection

With ActiveSheet '
LastRow = .Cells(.Rows.count, "B").End(xlUp).Row
Set rng = .Range("B27:B" & LastRow) 'non-contiguous range
End With

Set objChartSeriesColl = objChart.Chart.SeriesCollection


If frmGeneList.lstMain.ListIndex <> -1 Then 'if listbox is NOT empty
For l = 0 To frmGeneList.lstMain.ListCount - 1
If frmGeneList.lstMain.Selected(l) Then 'identify selected items
' count of cells in that range meeting criteria
iVal = Application.WorksheetFunction.CountIf(rng, frmGeneList.lstMain.List(l))

' Resize arrays to hold filtered data
ReDim MyArY(1 To iVal)
ReDim MyArX(1 To iVal)

iVal = 1

' Store filtered values from that range into array
For Each aCell In rng.Cells
If aCell.Value = frmGeneList.lstMain.List(l) Then
MyArY(iVal) = aCell.Offset(0, 1).Value'dates
MyArX(iVal) = aCell.Offset(0, 2).Value'numbers
iVal = iVal + 1
End If
Next aCell


xAddress_ListItem = frmGeneList.lstMain.List(l) '.Value
'defines series name

With objChartSeriesColl.NewSeries 'adds each? Series
.Name = xAddress_ListItem
.Values = MyArY
.XValues = MyArX
'.ApplyDataLabels
'.DataLabels.Position = xlLabelPositionAbove
'.DataLabels.NumberFormat = "0"
End With
        End If
    Next
End If

'objChart.HasTitle = True

With objChart.Chart
    '.Axes(xlCategory).TickLabels.NumberFormat = "m/d/yy;@" 'changes
'Xaxis text format
    .Axes(xlValue).TickLabels.NumberFormat = "General" 'changes Yaxis
'Text Format
    '.SetElement (msoElementChartTitleAboveChart) 'adds chart title above chart
    .SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
'adds Xaxis title
    .SetElement (msoElementPrimaryValueAxisTitleRotated) 'adds rotated
'Yaxis Title
    .SetElement (msoElementLegendBottom) 'adds legend @ bottom
    '.ChartTitle.Text = "IonTorrent Inter-Run Viriability"  'adds chart
'title above chart
    .Axes(xlCategory, xlPrimary).AxisTitle.Text = "Run Dates"
'renames Xaxis title to "X Title"
    '.Axes(xlValue, xlPrimary).AxisTitle.Text = "Sample Dates"
'renames Xaxis title to "X Title"
    .Axes(xlValue, xlPrimary).AxisTitle.Text = "%Alt" 'renames Yaxis
'title to "Y Title"
End With

With objChart.Chart.PlotArea.Format.Line 'adds black border around plot
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorText1
End With

With objChart.Chart.Legend.Format.Line 'adds black border around legend
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorText1
End With

End Sub

【问题讨论】:

  • 我刚刚开始研究 excel 图表。你的是高级编码。我在其中一篇文章中上传了一个基于@Meehow 代码的简单例程的示例散点系列图。您会在工作表 3 上注意到,可以通过 VBA 例程对齐相应的轴刻度并格式化刻度轴来显示正确的日期。dropbox.com/s/xg85k3vso5cedm6/chart3.xlsm?dl=0> 我也遇到了关于日期的类似问题。通过以下相关链接后,我可以改进它。quepublishing.com/articles/article.aspx?p=2023511&seqNum=2>
  • 另一个与日期问题相关的链接来自 Peltier 技术博客 peltiertech.com/link-excel-chart-axis-scale-to-values-in-cells>。
  • 我错过了ActiveSheet.ChartObjects(1).Activate 行。现在我已经激活了我正在使用的图表 With Activechart 并且我的标题出现了,但是,日期问题对我来说仍然是个谜。
  • 很高兴注意到您的进步。有两个与日期有关的问题。当我尝试时,它给出了 1900 年的日期。这是我拥有的 excel 2007 的基本开始日期。我将单元格格式化为通用格式,然后使用键 ~Ctrl+;~ 输入当前日期并复制到该范围内的其他单元格。第二个问题与轴比例有关,它由等间距以及最小值和最大值的原生特征定义。如果您在代码块中看到一个小的 VBA 例程已用于轴间距,使用工作表中指定的比例范围的最大值、最小值和平均值。

标签: excel charts vba


【解决方案1】:

'将 Date 转换为 Value/Double 然后转换为 Integer 以截断时间 MyArX(iVal) = Int(CDbl(aCell.Offset(0, 2).Value))

这对我有用。

【讨论】:

    猜你喜欢
    • 2022-06-10
    • 1970-01-01
    • 2012-10-11
    • 2012-05-10
    • 1970-01-01
    • 2017-04-14
    • 2017-10-16
    • 1970-01-01
    • 2010-10-03
    相关资源
    最近更新 更多