0
我不知道如何正確格式化日期(X軸),否則,下面的代碼工作。 MyArY()
包含日期,但由於某些原因,日期顯示爲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例程和格式化比例軸對齊來顯示正確的日期。我也遇到過類似的日期問題。我可以通過以下相關鏈接改進它。 –
skkakkar
另一個日期問題相關鏈接來自Peltier Tech Blog 。 –
skkakkar
我缺少'ActiveSheet.ChartObjects(1).Activate'行。現在我已經激活了我正在使用'使用Activechart'的圖表,並且我的標題已經顯示出來了,但是日期問題對我來說仍然很難理解。 – user3781528