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")。

 

就這樣吧

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM