簡體   English   中英

如何使用宏擴展圖形中包含的范圍

[英]How to extend the range that is included in a graph with a macro

我在A 列中有一些數據,我正在嘗試編寫一個宏以將數據范圍擴展到下一個 - B 列,這樣每次我在后續列中添加數據時,宏都會擴展邊框以將數據包含在圖表中plot。

請參見下圖,其中我的圖表中僅包含 A 列 - 在此處輸入圖像描述

我需要做什么才能讓它也包含下一列 - B 列

例如:

在此處輸入圖像描述

你怎么看?

這對我有用:

Sub ExpandChartSource()
    
    Dim ObjChart As Object
    Dim RngSource As Range
    
    Set ObjChart = ActiveSheet.ChartObjects(1)
    
    Set RngSource = Range(Split(ObjChart.Chart.SeriesCollection(1).Formula, ",")(2))
    
    Set RngSource = RngSource.Resize(RngSource.Rows.Count, RngSource.Columns.Count + 1)
    
    ObjChart.Chart.SetSourceData Source:=RngSource
    
End Sub

這里有一個更動態的版本,如果你想無限放大數據,它很有用:

Sub ExpandChartSource()
    
    Dim ObjChart As Object
    Dim RngSource As Range
    Dim IntSeries As Integer
    Dim StrAddress As String
    
    Set ObjChart = ActiveSheet.ChartObjects(1)
    
    Set RngSource = Range(Split(ObjChart.Chart.SeriesCollection(1).Formula, ",")(2))
    StrAddress = RngSource.Cells(1, 1).Address
    
    Set RngSource = Range(Split(ObjChart.Chart.SeriesCollection(ObjChart.Chart.SeriesCollection.Count).Formula, ",")(2))
    StrAddress = StrAddress & ":" & RngSource.Cells(RngSource.Rows.Count, 1).Address
    
    Set RngSource = Range(StrAddress)
    
    Set RngSource = RngSource.Resize(RngSource.Rows.Count, RngSource.Columns.Count + 1)
            
    ObjChart.Chart.SetSourceData Source:=RngSource
    
End Sub

它假定第一個系列是最左邊的系列,而最后一個系列被假定為最右邊的系列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM