我正在尝试通过使用VBA创建折线图,我通过使用随附的代码来完成此操作,我在rng中列出了我想要的类别,但是它并不会自动将其选择为我想要的类别栏4,5将显示在图表上,而3将作为类别。
Sub CreateGBPChart()
'PURPOSE: Create a chart (chart dimensions are not required)
Dim rng As Range
Dim cht As Object
Dim wks As Worksheet, wks2 As Worksheet
Set wks = Sheet2
Set wks2 = Sheet5
'data range for the chart
lastrow = wks.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Set rng = Range(wks.Cells(10, 3), wks.Cells(lastrow, 5))
'Create a chart
Set cht = wks2.ChartObjects.Add( _
Left:=wks2.Range("K18").Left, _
Width:=480, _
Top:=wks2.Range("K18").Top, _
Height:=200)
' chart data
cht.Chart.SetSourceData Source:=rng
'cht.Chart.Axes(xlCategory).CategoryNames = Range(wks.Cells(10, 3), wks.Cells(lastrow, 3))
'Determine the chart type
cht.Chart.ChartType = xlXYScatterLines
'chart title
cht.Chart.HasTitle = True
'Change title
cht.Chart.ChartTitle.Text = "GBP Price"
'Remove Legend
'cht.Legend.Delete
cht.Chart.Parent.Name = "GBPTable"
'adjust the min val
cht.Axes(xlValue).MinimumScale = 1
'format fontsize'
cht.Chart.ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
'cht.Chart.chartSeriesCollection(1).Line.Width = 4
'cht.Chart.chartSeriesCollection(2).Format.Line.Width = 4
End Sub
尝试一下:
' cht.Chart.SetSourceData Source:=rng
cht.Chart.SetSourceData Source:=wks.Range(wks.Cells(10, 4), wks.Cells(LastRow, 5))
' Add lines
cht.Chart.SeriesCollection(1).XValues = wks.Range(wks.Cells(10, 3), wks.Cells(LastRow, 3))
cht.Chart.SeriesCollection(2).XValues = wks.Range(wks.Cells(10, 3), wks.Cells(LastRow, 3))