宏生成图表

If ActiveCell.Text = "" Then Exit Sub
Dim Startrowno As Integer, Startcolno As Integer, Endrowno As Integer, Endcolno As Integer
Startrowno = Target.Row
Startcolno = Target.Column
Endrowno = Target.Rows.Count + Startrowno - 1
Endcolno = Target.Columns.Count + Startcolno - 1
ActiveSheet.Name = "宏Excel Chart"
ActiveSheet.Cells.Item(1, 1) = "Excel Chart例子"

'向工作表中添加內嵌圖表﹐Add方法中的四個參數分別表示與儲存格A1的左邊距﹑頂部邊距﹑以及圖表的寬度and高度﹔
'ActiveSheet.Shapes.AddChart.Select
ActiveSheet.ChartObjects.Add 0, 0, 500, 300

ActiveSheet.ChartObjects(1).Activate '激活當前圖表Range(Cells(2, 1), Cells(3, 8))
 ActiveChart.SetSourceData Source:=Range(Cells(Startrowno, Startcolno), Cells(Endrowno, Endcolno))
 ActiveChart.ChartType = xlLine


'ActiveSheet.ChartObjects(1).Chart.Axes(xlValue).MinimumScale = 100 '設定數值座標軸的最小刻度值﹔
'ActiveSheet.ChartObjects(1).Chart.Axes(xlValue).MaximumScale = 200 '設定數值座標軸的最大刻度值﹔
'ActiveSheet.ChartObjects(1).Chart.Axes(xlValue).MajorUnit = 10 '設定數值座標的主要單位﹔
'ActiveSheet.ChartObjects(1).Chart.Axes(xlValue).MinorUnit = 10 '設定數值座標的次要單位﹔
ActiveSheet.ChartObjects(1).Chart.Axes(xlCategory).HasTitle = True
ActiveSheet.ChartObjects(1).Chart.Axes(xlCategory).AxisTitle.Text = "星期"  '類別座標軸標簽。
ActiveSheet.ChartObjects(1).Chart.Axes(xlCategory).CategoryNames = Range(Cells(2, 2), Cells(2, 8))
'ActiveSheet.ChartObjects(1).Chart.HasLegend = False '不顯示圖例
'ActiveSheet.ChartObjects(1).Chart.ChartArea.Fill.Visible = True '圖表區域填滿
ActiveSheet.ChartObjects(1).Chart.ChartArea.Fill.ForeColor.SchemeColor = 28   '前景色彩﹔
ActiveSheet.ChartObjects(1).Chart.ChartArea.Fill.BackColor.SchemeColor = 42   '背景色彩﹔
'ActiveSheet.ChartObjects(1).Chart.Rotation = 44 ' 以度為單位傳回或設定立體圖表檢視的旋轉值﹔
''ActiveSheet.ChartObjects(1).Chart.Walls.Interior.ColorIndex = 28
''如果指定圖表的座標軸為直角﹐并與圖表的轉角或仰角無關﹐則為True,僅適用于立體折線圖﹐直條圖與橫條圖﹔
''ActiveSheet.ChartObjects(1).Chart.RightAngleAxes = True
'ActiveSheet.ChartObjects(1).Chart.ChartGroups(1).VaryByCategories = True '對每個資料標號指定不同的色彩或圖樣
With ActiveChart.SeriesCollection.NewSeries
    .Name = ActiveSheet.Range("A2")
    .Values = ActiveSheet.Range("A3:A4")
    .XValues = ActiveSheet.Range("B2:H2")
End With
ActiveChart.SeriesCollection(Target.Rows.Count).Delete
For i = 1 To Target.Rows.Count - 1
ActiveChart.SeriesCollection(i).HasDataLabels = True '顯示圖表中數列的資料標簽
Next i

原文地址:https://www.cnblogs.com/bjxly/p/2100517.html