1. 程式人生 > >excel中批量生成圖表的指令碼

excel中批量生成圖表的指令碼

經常要根據一堆資料生成各種bug曲線的報表,所以就抽時間完成了這個指令碼,有需求的可以拿去自行修改
Function getcolname(ByVal intcol As Long)
    intcol = intcol - 1
    getcolname = IIf(intcol \ 26, Chr(64 + intcol \ 26), "") & Chr(65 + intcol Mod 26)
End Function

Sub 批量生成圖表()
'
' 批量生成圖表
' by marty
' 原始資料需要儲存在sheet1中

    x = 0
    y = 0
    Sheets.Add After:=Sheets(Sheets.Count)
    'ActiveSheet.Name = "Bug圖表"
    '獲取sheet1中總的列數
    intcol = Sheets("Sheet1").Range("IV1").End(xlToLeft).Column
    strCol = getcolname(intcol)
    '生成圖表
    For i = 2 To 6
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlLineMarkers
        ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("C" & i & ":" & strCol & i)
        ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$C$1:$" & strCol & "$1"
        ActiveChart.SeriesCollection(1).Name = "=Sheet1!$B$" & i
        '設定x軸的型別為文字座標軸,而不是時間座標軸
        ActiveChart.Axes(xlCategory).Select
        ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
    Next i
    '調整每個圖表的位置
    For Each Chart In ActiveSheet.ChartObjects
        Chart.Activate
        ActiveChart.ChartArea.Select
            Chart.Top = y * 206
            Chart.Left = x * 606
            Chart.Height = 200
            Chart.Width = 600
        x = x + 1
        If x >= 1 Then
            x = 0
            y = y + 1
        End If
    Next Chart
    
End Sub