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   风中狂笑  阅读(1928)  评论(2编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示