簡體   English   中英

使用 VBA 自動生成圖表

[英]Automating production of graphs using VBA

我有大量的圖表要制作,我試圖避免手動創建和格式化它們。 數據我已經鏈接了上面數據的圖像,為了數據安全省略了行標簽。 我需要每個時間序列數據行的折線圖,其中每個圖的標題是最左側列中的行 label,x 軸標簽是列標簽。 我能夠在 for 循環中創建圖表,但在分配圖表標題和 x 軸標簽時遇到問題。 我已經閱讀了其他帖子,但修復沒有幫助。 到目前為止的代碼如下:

Sub main()

    Dim i As Long
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim chrt As Chart
    Dim chrtname As String

    'Find the last used row
    'LastRow = Sheets("Chart Data").Range("A1").End(xlUp).Row
    LastRow = 272
    'Find the last used column
    'LastColumn = Sheets("Chart Data").Range("A1").End(xlToRight).Column
    LastColumn = 15
    'Looping from second row till last row which has the data
    For i = 86 To LastRow
        Sheets("Sheet2").Select

        'Adds chart to the sheet
        Set chrt = Sheets("Sheet2").Shapes.AddChart.Chart
        'sets the chart type
        chrt.ChartType = xlLine

        'adding line chart
        With Sheets("Charts Data")
            chrt.SetSourceData Source:=.Range(.Cells(i, 3), .Cells(i, LastColumn))
        End With

        'adjust the position of chart on sheet
        chrt.ChartArea.Left = 1
        chrt.ChartArea.Top = (i - (i *0.5)) * chrt.ChartArea.Height
        
        'Trying to set a chart title
        chrt.HasTitle = True
        chrtname = Cells(i, 2).Value
        chrt.SetElement (msoElementChartTitleAboveChart)
        chrt.ChartTitle.Select
        chrt.ChartTitle.Text = chrtname
        
        'Trying to add x axis labels
        chrt.SeriesCollection(1).XValues = "=Charts Data!$C$3:$O$3"
    
    
        Next

End Sub

如果我在“”中鍵入圖表標題的文本,它會起作用,但在嘗試分配單元格值時,圖表標題似乎會被刪除,另一個用戶似乎也有類似的問題。 如何使用 vba 將單元格值設為圖表標題

這是私下解決的,因此任何指導表示贊賞。 我還需要添加一個額外的數據系列,它在所有圖表中都是相同的,但我也沒有成功。

我很抱歉,因為這可能是一個非常基本的問題,但我是 VBA 的新手。

對於標題問題,您必須更改:

chrtname = Cells(i, 2).Value

到:

chrtname = Sheets("Charts Data").Cells(i, 2).Value

由於沒有明確引用工作表父級 object,因此范圍 object 將隱式假定當前活動工作表,您通過Sheets("Sheet2").Select語句將其設為“Sheet2”

而對於 XValues 問題,您必須更改:

chrt.SeriesCollection(1).XValues = "=Charts Data!$C$3:$O$3"

到:

chrt.SeriesCollection(1).XValues = "='Charts Data'!$C$3:$Q$3"

因為帶有空格的工作表名稱必須用單引號括起來

但是您可能想考慮對代碼進行這種重構,它廣泛使用With.. End With結構,以便更好地掌握您當前“持有”哪個 object

Option Explicit

Sub main()

    Dim i As Long
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim chrt As Chart

    'Find the last used row
    'LastRow = Sheets("Chart Data").Range("A1").End(xlUp).Row
    LastRow = 272
    'Find the last used column
    'LastColumn = Sheets("Chart Data").Range("A1").End(xlToRight).Column
    LastColumn = 15
    'Looping from second row till last row which has the data
    
    With Sheets("Charts Data") ' reference data sheet
        For i = 86 To LastRow
    
            'Adds chart to the Sheet2
            Set chrt = Sheets("Sheet2").Shapes.AddChart.Chart
            
            'adding line chart
            chrt.SetSourceData Source:=.Range(.Cells(i, 3), .Cells(i, LastColumn))
            
            With chrt ' reference the just added chart

                'sets the chart type
                .ChartType = xlLine
        
                'adjust the position of chart on sheet
                .ChartArea.Left = 1
                .ChartArea.Top = (i - (i * 0.5)) * chrt.ChartArea.Height
                
                'Trying to set a chart title
                .HasTitle = True
                
                'add x axis labels
                .SeriesCollection(1).XValues = "='Charts Data'!$C$3:$Q$3"

            End With
            
            chrt.ChartTitle.Text = .Cells(i, 2).Value
            
        Next
    End With

End Sub

暫無
暫無

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

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