vba+excel+chart绘图

官方帮助

ChartObjects 对象 (Excel) | Microsoft Docs

首先,是在excel的工作表中插入一个散点连线图,比如这种,这个就是一个ChartObject对象,里面嵌入一个图表Chart,也就是Chart需要ChartObject这么一个容器

Sub 宏2()
    Dim oWb As Object: Set oWb = ThisWorkbook
    Dim oSht As Object: Set oSht = ActiveSheet
    
    Dim oChartObject As Object
    Dim oChart As Object: Set oChart = ActiveSheet.ChartObjects("图表 1").Chart
    
    Dim ct As Integer
    
    ''图表上的数据数量
    ct = oChart.SeriesCollection.Count
    
    '''delete the series on chart in reverse
    If ct > 0 Then
        For ind = ct To 1 Step -1
            oChart.SeriesCollection(ind).Delete
        Next
    End If
        
        
    Dim oNSeries As Object
    Dim strShtName As String
    strShtName = "='表名'"
    
    For Row = 4 To 15
        Set oNSeries = oChart.SeriesCollection.NewSeries
        oNSeries.Name = strShtName + "!$B$" + CStr(Row)
        oNSeries.Values = strShtName + "!$U$" + CStr(Row) + ":$X$" + CStr(Row)
        oNSeries.Format.Fill.ForeColor.RGB = rgbBlack     
        oNSeries.Format.Line.ForeColor.RGB = rgbBlack
              
    Next Row
    oNSeries.XValues = Array("1", "2", "3", "4")
  
End Sub

因为这个表是在代码之前生成的,我们所要做的是在这个表上进行绘图,通过宏录制,可以知道改工作表的嵌入图表名为ActiveSheet.ChartObjects("图表 1")。

 

就这样吧

 

posted on 2021-08-31 14:28  风中狂笑  阅读(1806)  评论(2编辑  收藏  举报

导航