我尝试使用“ A1”作为顶部单元格,然后使用此数据在每月租金和购买量之间进行比较。
Option Explicit
Dim isChart As Boolean ' True = chart added; False = chart deleted
Dim isRent As Boolean ' True = column clustered; False = line markers
Dim isBuy As Boolean ' True = column clustered; False = line markers
Dim isTitle As Boolean ' True = visible; False = hide
Dim isLegend As Boolean ' True = visible; False = hide
' 2. Implement click event handler for the Chart button. (+20pts)
Sub ButtonChart_Click()
Dim chtObject As ChartObject
Dim cht As Chart
Dim sc As SeriesCollection
Dim ser As Series
Dim topCell As Range
If isChart Then Sheet2.ChartObjects.Delete
Set chtObject = ThisWorkbook.Worksheets("Chart").ChartObjects.Add(Range("D3").Left, Range("D3").Top, Range("D3:M20").Width, Range("M3:M20").Height)
chtObject.Name = "Monthly"
isChart = True
Set cht = chtObject.Chart
Set topCell = Range("A1")
With cht
.HasLegend = True
.HasTitle = True
.ChartTitle.Text = "Monthly Average"
Set sc = .SeriesCollection
Set ser = sc.NewSeries
With ser
.Name = topCell.Offset(1, 1)
.XValues = Range(topCell.Offset(2, 0), topCell.Offset(2, 0).End(xlDown))
.Values = Range(topCell.Offset(2, 1), topCell.Offset(2, 1).End(xlDown))
.ChartType = xlLineMarkers
End With
Set ser = sc.NewSeries
With ser
.Name = topCell.Offset(1, 2)
.Values = Range(topCell.Offset(2, 2), topCell.Offset(2, 2).End(xlDown))
.ChartType = xlColumnClustered
.AxisGroup = xlSecondary
End With
End With
End Sub